2 Copyright (C) 1987-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 /* Disable rtl checking; it conflicts with the iterator handling. */
23 #undef ENABLE_RTL_CHECKING
26 #include "coretypes.h"
31 #include "gensupport.h"
33 /* One element in a singly-linked list of (integer, string) pairs. */
35 struct map_value
*next
;
40 /* Maps an iterator or attribute name to a list of (integer, string) pairs.
41 The integers are iterator values; the strings are either C conditions
42 or attribute values. */
44 /* The name of the iterator or attribute. */
47 /* The group (modes or codes) to which the iterator or attribute belongs. */
48 struct iterator_group
*group
;
50 /* The list of (integer, string) pairs. */
51 struct map_value
*values
;
53 /* For iterators, records the current value of the iterator. */
54 struct map_value
*current_value
;
57 /* A structure for abstracting the common parts of iterators. */
58 struct iterator_group
{
59 /* Tables of "mapping" structures, one for attributes and one for
61 htab_t attrs
, iterators
;
63 /* Treat the given string as the name of a standard mode, etc., and
64 return its integer value. */
65 int (*find_builtin
) (const char *);
67 /* Make the given pointer use the given iterator value. */
68 void (*apply_iterator
) (void *, int);
71 /* Records one use of an iterator. */
73 /* The iterator itself. */
74 struct mapping
*iterator
;
76 /* The location of the use, as passed to the apply_iterator callback. */
80 /* Records one use of an attribute (the "<[iterator:]attribute>" syntax)
81 in a non-string rtx field. */
82 struct attribute_use
{
83 /* The group that describes the use site. */
84 struct iterator_group
*group
;
86 /* The name of the attribute, possibly with an "iterator:" prefix. */
89 /* The location of the use, as passed to GROUP's apply_iterator callback. */
93 /* This struct is used to link subst_attr named ATTR_NAME with
94 corresponding define_subst named ITER_NAME. */
95 struct subst_attr_to_iter_mapping
101 /* Hash-table to store links between subst-attributes and
103 htab_t subst_attr_to_iter_map
= NULL
;
104 /* This global stores name of subst-iterator which is currently being
106 const char *current_iterator_name
;
108 static void validate_const_int (const char *);
110 /* The mode and code iterator structures. */
111 static struct iterator_group modes
, codes
, ints
, substs
;
113 /* All iterators used in the current rtx. */
114 static vec
<mapping
*> current_iterators
;
116 /* The list of all iterator uses in the current rtx. */
117 static vec
<iterator_use
> iterator_uses
;
119 /* The list of all attribute uses in the current rtx. */
120 static vec
<attribute_use
> attribute_uses
;
122 /* Implementations of the iterator_group callbacks for modes. */
125 find_mode (const char *name
)
129 for (i
= 0; i
< NUM_MACHINE_MODES
; i
++)
130 if (strcmp (GET_MODE_NAME (i
), name
) == 0)
133 fatal_with_file_and_line ("unknown mode `%s'", name
);
137 apply_mode_iterator (void *loc
, int mode
)
139 PUT_MODE ((rtx
) loc
, (machine_mode
) mode
);
142 /* Implementations of the iterator_group callbacks for codes. */
145 find_code (const char *name
)
149 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
150 if (strcmp (GET_RTX_NAME (i
), name
) == 0)
153 fatal_with_file_and_line ("unknown rtx code `%s'", name
);
157 apply_code_iterator (void *loc
, int code
)
159 PUT_CODE ((rtx
) loc
, (enum rtx_code
) code
);
162 /* Implementations of the iterator_group callbacks for ints. */
164 /* Since GCC does not construct a table of valid constants,
165 we have to accept any int as valid. No cross-checking can
169 find_int (const char *name
)
171 validate_const_int (name
);
176 apply_int_iterator (void *loc
, int value
)
181 /* This routine adds attribute or does nothing depending on VALUE. When
182 VALUE is 1, it does nothing - the first duplicate of original
183 template is kept untouched when it's subjected to a define_subst.
184 When VALUE isn't 1, the routine modifies RTL-template LOC, adding
185 attribute, named exactly as define_subst, which later will be
186 applied. If such attribute has already been added, then no the
187 routine has no effect. */
189 apply_subst_iterator (void *loc
, int value
)
193 rtvec attrs_vec
, new_attrs_vec
;
197 gcc_assert (GET_CODE (rt
) == DEFINE_INSN
198 || GET_CODE (rt
) == DEFINE_EXPAND
);
200 attrs_vec
= XVEC (rt
, 4);
202 /* If we've already added attribute 'current_iterator_name', then we
203 have nothing to do now. */
206 for (i
= 0; i
< GET_NUM_ELEM (attrs_vec
); i
++)
208 if (strcmp (XSTR (attrs_vec
->elem
[i
], 0), current_iterator_name
) == 0)
213 /* Add attribute with subst name - it serves as a mark for
214 define_subst which later would be applied to this pattern. */
215 new_attr
= rtx_alloc (SET_ATTR
);
216 PUT_CODE (new_attr
, SET_ATTR
);
217 XSTR (new_attr
, 0) = xstrdup (current_iterator_name
);
218 XSTR (new_attr
, 1) = xstrdup ("yes");
222 new_attrs_vec
= rtvec_alloc (1);
223 new_attrs_vec
->elem
[0] = new_attr
;
227 new_attrs_vec
= rtvec_alloc (GET_NUM_ELEM (attrs_vec
) + 1);
228 memcpy (&new_attrs_vec
->elem
[0], &attrs_vec
->elem
[0],
229 GET_NUM_ELEM (attrs_vec
) * sizeof (rtx
));
230 new_attrs_vec
->elem
[GET_NUM_ELEM (attrs_vec
)] = new_attr
;
232 XVEC (rt
, 4) = new_attrs_vec
;
235 /* Map subst-attribute ATTR to subst iterator ITER. */
238 bind_subst_iter_and_attr (const char *iter
, const char *attr
)
240 struct subst_attr_to_iter_mapping
*value
;
242 if (!subst_attr_to_iter_map
)
243 subst_attr_to_iter_map
=
244 htab_create (1, leading_string_hash
, leading_string_eq_p
, 0);
245 value
= XNEW (struct subst_attr_to_iter_mapping
);
246 value
->attr_name
= xstrdup (attr
);
247 value
->iter_name
= xstrdup (iter
);
248 slot
= htab_find_slot (subst_attr_to_iter_map
, value
, INSERT
);
252 /* Return name of a subst-iterator, corresponding to subst-attribute ATTR. */
255 find_subst_iter_by_attr (const char *attr
)
257 char *iter_name
= NULL
;
258 struct subst_attr_to_iter_mapping
*value
;
259 value
= (struct subst_attr_to_iter_mapping
*)
260 htab_find (subst_attr_to_iter_map
, &attr
);
262 iter_name
= value
->iter_name
;
266 /* Map attribute string P to its current value. Return null if the attribute
269 static struct map_value
*
270 map_attr_string (const char *p
)
273 struct mapping
*iterator
;
277 int iterator_name_len
;
279 /* Peel off any "iterator:" prefix. Set ATTR to the start of the
281 attr
= strchr (p
, ':');
284 iterator_name_len
= -1;
289 iterator_name_len
= attr
- p
;
293 FOR_EACH_VEC_ELT (current_iterators
, i
, iterator
)
295 /* If an iterator name was specified, check that it matches. */
296 if (iterator_name_len
>= 0
297 && (strncmp (p
, iterator
->name
, iterator_name_len
) != 0
298 || iterator
->name
[iterator_name_len
] != 0))
301 /* Find the attribute specification. */
302 m
= (struct mapping
*) htab_find (iterator
->group
->attrs
, &attr
);
305 /* In contrast to code/mode/int iterators, attributes of subst
306 iterators are linked to one specific subst-iterator. So, if
307 we are dealing with subst-iterator, we should check if it's
308 the one which linked with the given attribute. */
309 if (iterator
->group
== &substs
)
311 char *iter_name
= find_subst_iter_by_attr (attr
);
312 if (strcmp (iter_name
, iterator
->name
) != 0)
315 /* Find the attribute value associated with the current
317 for (v
= m
->values
; v
; v
= v
->next
)
318 if (v
->number
== iterator
->current_value
->number
)
325 /* Apply the current iterator values to STRING. Return the new string
326 if any changes were needed, otherwise return STRING itself. */
329 rtx_reader::apply_iterator_to_string (const char *string
)
331 char *base
, *copy
, *p
, *start
, *end
;
337 base
= p
= copy
= ASTRDUP (string
);
338 while ((start
= strchr (p
, '<')) && (end
= strchr (start
, '>')))
343 v
= map_attr_string (p
);
348 /* Add everything between the last copied byte and the '<',
349 then add in the attribute value. */
350 obstack_grow (&m_string_obstack
, base
, start
- base
);
351 obstack_grow (&m_string_obstack
, v
->string
, strlen (v
->string
));
356 obstack_grow (&m_string_obstack
, base
, strlen (base
) + 1);
357 copy
= XOBFINISH (&m_string_obstack
, char *);
358 copy_md_ptr_loc (copy
, string
);
364 /* Return a deep copy of X, substituting the current iterator
365 values into any strings. */
368 rtx_reader::copy_rtx_for_iterators (rtx original
)
370 const char *format_ptr
, *p
;
377 /* Create a shallow copy of ORIGINAL. */
378 x
= rtx_alloc (GET_CODE (original
));
379 memcpy (x
, original
, RTX_CODE_SIZE (GET_CODE (original
)));
381 /* Change each string and recursively change each rtx. */
382 format_ptr
= GET_RTX_FORMAT (GET_CODE (original
));
383 for (i
= 0; format_ptr
[i
] != 0; i
++)
384 switch (format_ptr
[i
])
387 while (XTMPL (x
, i
) != (p
= apply_iterator_to_string (XTMPL (x
, i
))))
393 while (XSTR (x
, i
) != (p
= apply_iterator_to_string (XSTR (x
, i
))))
398 XEXP (x
, i
) = copy_rtx_for_iterators (XEXP (x
, i
));
403 if (XVEC (original
, i
))
405 XVEC (x
, i
) = rtvec_alloc (XVECLEN (original
, i
));
406 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
408 = copy_rtx_for_iterators (XVECEXP (original
, i
, j
));
418 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
419 has the form "&& ..." (as used in define_insn_and_splits), assume that
420 EXTRA is already satisfied. Empty strings are treated like "true". */
423 add_condition_to_string (const char *original
, const char *extra
)
425 if (original
!= 0 && original
[0] == '&' && original
[1] == '&')
427 return rtx_reader_ptr
->join_c_conditions (original
, extra
);
430 /* Like add_condition, but applied to all conditions in rtx X. */
433 add_condition_to_rtx (rtx x
, const char *extra
)
435 switch (GET_CODE (x
))
440 XSTR (x
, 2) = add_condition_to_string (XSTR (x
, 2), extra
);
444 case DEFINE_PEEPHOLE
:
445 case DEFINE_PEEPHOLE2
:
446 case DEFINE_COND_EXEC
:
447 XSTR (x
, 1) = add_condition_to_string (XSTR (x
, 1), extra
);
450 case DEFINE_INSN_AND_SPLIT
:
451 XSTR (x
, 2) = add_condition_to_string (XSTR (x
, 2), extra
);
452 XSTR (x
, 4) = add_condition_to_string (XSTR (x
, 4), extra
);
460 /* Apply the current iterator values to all attribute_uses. */
463 apply_attribute_uses (void)
469 FOR_EACH_VEC_ELT (attribute_uses
, i
, ause
)
471 v
= map_attr_string (ause
->value
);
473 fatal_with_file_and_line ("unknown iterator value `%s'", ause
->value
);
474 ause
->group
->apply_iterator (ause
->ptr
,
475 ause
->group
->find_builtin (v
->string
));
479 /* A htab_traverse callback for iterators. Add all used iterators
480 to current_iterators. */
483 add_current_iterators (void **slot
, void *data ATTRIBUTE_UNUSED
)
485 struct mapping
*iterator
;
487 iterator
= (struct mapping
*) *slot
;
488 if (iterator
->current_value
)
489 current_iterators
.safe_push (iterator
);
493 /* Expand all iterators in the current rtx, which is given as ORIGINAL.
494 Build a list of expanded rtxes in the EXPR_LIST pointed to by QUEUE. */
497 apply_iterators (rtx original
, vec
<rtx
> *queue
)
500 const char *condition
;
502 struct mapping
*iterator
;
506 if (iterator_uses
.is_empty ())
508 /* Raise an error if any attributes were used. */
509 apply_attribute_uses ();
510 queue
->safe_push (original
);
514 /* Clear out the iterators from the previous run. */
515 FOR_EACH_VEC_ELT (current_iterators
, i
, iterator
)
516 iterator
->current_value
= NULL
;
517 current_iterators
.truncate (0);
519 /* Mark the iterators that we need this time. */
520 FOR_EACH_VEC_ELT (iterator_uses
, i
, iuse
)
521 iuse
->iterator
->current_value
= iuse
->iterator
->values
;
523 /* Get the list of iterators that are in use, preserving the
524 definition order within each group. */
525 htab_traverse (modes
.iterators
, add_current_iterators
, NULL
);
526 htab_traverse (codes
.iterators
, add_current_iterators
, NULL
);
527 htab_traverse (ints
.iterators
, add_current_iterators
, NULL
);
528 htab_traverse (substs
.iterators
, add_current_iterators
, NULL
);
529 gcc_assert (!current_iterators
.is_empty ());
533 /* Apply the current iterator values. Accumulate a condition to
534 say when the resulting rtx can be used. */
536 FOR_EACH_VEC_ELT (iterator_uses
, i
, iuse
)
538 if (iuse
->iterator
->group
== &substs
)
540 v
= iuse
->iterator
->current_value
;
541 iuse
->iterator
->group
->apply_iterator (iuse
->ptr
, v
->number
);
542 condition
= rtx_reader_ptr
->join_c_conditions (condition
, v
->string
);
544 apply_attribute_uses ();
545 x
= rtx_reader_ptr
->copy_rtx_for_iterators (original
);
546 add_condition_to_rtx (x
, condition
);
548 /* We apply subst iterator after RTL-template is copied, as during
549 subst-iterator processing, we could add an attribute to the
550 RTL-template, and we don't want to do it in the original one. */
551 FOR_EACH_VEC_ELT (iterator_uses
, i
, iuse
)
553 v
= iuse
->iterator
->current_value
;
554 if (iuse
->iterator
->group
== &substs
)
557 current_iterator_name
= iuse
->iterator
->name
;
558 iuse
->iterator
->group
->apply_iterator (iuse
->ptr
, v
->number
);
561 /* Add the new rtx to the end of the queue. */
562 queue
->safe_push (x
);
564 /* Lexicographically increment the iterator value sequence.
565 That is, cycle through iterator values, starting from the right,
566 and stopping when one of them doesn't wrap around. */
567 i
= current_iterators
.length ();
573 iterator
= current_iterators
[i
];
574 iterator
->current_value
= iterator
->current_value
->next
;
575 if (iterator
->current_value
)
577 iterator
->current_value
= iterator
->values
;
582 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
583 of the mapping and GROUP is the group to which it belongs. */
585 static struct mapping
*
586 add_mapping (struct iterator_group
*group
, htab_t table
, const char *name
)
591 m
= XNEW (struct mapping
);
592 m
->name
= xstrdup (name
);
595 m
->current_value
= NULL
;
597 slot
= htab_find_slot (table
, m
, INSERT
);
599 fatal_with_file_and_line ("`%s' already defined", name
);
605 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
606 END_PTR points to the current null terminator for the list; return
607 a pointer the new null terminator. */
609 static struct map_value
**
610 add_map_value (struct map_value
**end_ptr
, int number
, const char *string
)
612 struct map_value
*value
;
614 value
= XNEW (struct map_value
);
616 value
->number
= number
;
617 value
->string
= string
;
623 /* Do one-time initialization of the mode and code attributes. */
626 initialize_iterators (void)
628 struct mapping
*lower
, *upper
;
629 struct map_value
**lower_ptr
, **upper_ptr
;
633 modes
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
634 modes
.iterators
= htab_create (13, leading_string_hash
,
635 leading_string_eq_p
, 0);
636 modes
.find_builtin
= find_mode
;
637 modes
.apply_iterator
= apply_mode_iterator
;
639 codes
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
640 codes
.iterators
= htab_create (13, leading_string_hash
,
641 leading_string_eq_p
, 0);
642 codes
.find_builtin
= find_code
;
643 codes
.apply_iterator
= apply_code_iterator
;
645 ints
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
646 ints
.iterators
= htab_create (13, leading_string_hash
,
647 leading_string_eq_p
, 0);
648 ints
.find_builtin
= find_int
;
649 ints
.apply_iterator
= apply_int_iterator
;
651 substs
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
652 substs
.iterators
= htab_create (13, leading_string_hash
,
653 leading_string_eq_p
, 0);
654 substs
.find_builtin
= find_int
; /* We don't use it, anyway. */
655 substs
.apply_iterator
= apply_subst_iterator
;
657 lower
= add_mapping (&modes
, modes
.attrs
, "mode");
658 upper
= add_mapping (&modes
, modes
.attrs
, "MODE");
659 lower_ptr
= &lower
->values
;
660 upper_ptr
= &upper
->values
;
661 for (i
= 0; i
< MAX_MACHINE_MODE
; i
++)
663 copy
= xstrdup (GET_MODE_NAME (i
));
664 for (p
= copy
; *p
!= 0; p
++)
667 upper_ptr
= add_map_value (upper_ptr
, i
, GET_MODE_NAME (i
));
668 lower_ptr
= add_map_value (lower_ptr
, i
, copy
);
671 lower
= add_mapping (&codes
, codes
.attrs
, "code");
672 upper
= add_mapping (&codes
, codes
.attrs
, "CODE");
673 lower_ptr
= &lower
->values
;
674 upper_ptr
= &upper
->values
;
675 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
677 copy
= xstrdup (GET_RTX_NAME (i
));
678 for (p
= copy
; *p
!= 0; p
++)
681 lower_ptr
= add_map_value (lower_ptr
, i
, GET_RTX_NAME (i
));
682 upper_ptr
= add_map_value (upper_ptr
, i
, copy
);
686 /* Provide a version of a function to read a long long if the system does
688 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !HAVE_DECL_ATOLL && !defined(HAVE_ATOQ)
689 HOST_WIDE_INT
atoll (const char *);
692 atoll (const char *p
)
695 HOST_WIDE_INT tmp_wide
;
707 HOST_WIDE_INT new_wide
= tmp_wide
*10 + (*p
- '0');
708 if (new_wide
< tmp_wide
)
710 /* Return INT_MAX equiv on overflow. */
711 tmp_wide
= HOST_WIDE_INT_M1U
>> 1;
719 tmp_wide
= -tmp_wide
;
724 /* Process a define_conditions directive, starting with the optional
725 space after the "define_conditions". The directive looks like this:
733 It's not intended to appear in machine descriptions. It is
734 generated by (the program generated by) genconditions.c, and
735 slipped in at the beginning of the sequence of MD files read by
736 most of the other generators. */
738 rtx_reader::read_conditions ()
742 require_char_ws ('[');
744 while ( (c
= read_skip_spaces ()) != ']')
751 fatal_expected_char ('(', c
);
754 validate_const_int (name
.string
);
755 value
= atoi (name
.string
);
757 require_char_ws ('"');
758 expr
= read_quoted_string ();
760 require_char_ws (')');
762 add_c_test (expr
, value
);
767 validate_const_int (const char *string
)
773 while (*cp
&& ISSPACE (*cp
))
775 if (*cp
== '-' || *cp
== '+')
786 fatal_with_file_and_line ("invalid decimal constant \"%s\"\n", string
);
790 validate_const_wide_int (const char *string
)
796 while (*cp
&& ISSPACE (*cp
))
798 /* Skip the leading 0x. */
799 if (cp
[0] == '0' || cp
[1] == 'x')
806 if (! ISXDIGIT (*cp
))
809 fatal_with_file_and_line ("invalid hex constant \"%s\"\n", string
);
812 /* Record that PTR uses iterator ITERATOR. */
815 record_iterator_use (struct mapping
*iterator
, void *ptr
)
817 struct iterator_use iuse
= {iterator
, ptr
};
818 iterator_uses
.safe_push (iuse
);
821 /* Record that PTR uses attribute VALUE, which must match a built-in
822 value from group GROUP. */
825 record_attribute_use (struct iterator_group
*group
, void *ptr
,
828 struct attribute_use ause
= {group
, value
, ptr
};
829 attribute_uses
.safe_push (ause
);
832 /* Interpret NAME as either a built-in value, iterator or attribute
833 for group GROUP. PTR is the value to pass to GROUP's apply_iterator
837 rtx_reader::record_potential_iterator_use (struct iterator_group
*group
,
838 void *ptr
, const char *name
)
844 if (name
[0] == '<' && name
[len
- 1] == '>')
846 /* Copy the attribute string into permanent storage, without the
847 angle brackets around it. */
848 obstack_grow0 (&m_string_obstack
, name
+ 1, len
- 2);
849 record_attribute_use (group
, ptr
, XOBFINISH (&m_string_obstack
, char *));
853 m
= (struct mapping
*) htab_find (group
->iterators
, &name
);
855 record_iterator_use (m
, ptr
);
857 group
->apply_iterator (ptr
, group
->find_builtin (name
));
861 /* Finish reading a declaration of the form:
863 (define... <name> [<value1> ... <valuen>])
865 from the MD file, where each <valuei> is either a bare symbol name or a
866 "(<name> <string>)" pair. The "(define..." part has already been read.
868 Represent the declaration as a "mapping" structure; add it to TABLE
869 (which belongs to GROUP) and return it. */
872 rtx_reader::read_mapping (struct iterator_group
*group
, htab_t table
)
876 struct map_value
**end_ptr
;
880 /* Read the mapping name and create a structure for it. */
882 m
= add_mapping (group
, table
, name
.string
);
884 require_char_ws ('[');
886 /* Read each value. */
887 end_ptr
= &m
->values
;
888 c
= read_skip_spaces ();
893 /* A bare symbol name that is implicitly paired to an
901 /* A "(name string)" pair. */
903 string
= read_string (false);
904 require_char_ws (')');
906 number
= group
->find_builtin (name
.string
);
907 end_ptr
= add_map_value (end_ptr
, number
, string
);
908 c
= read_skip_spaces ();
915 /* For iterator with name ATTR_NAME generate define_attr with values
916 'yes' and 'no'. This attribute is used to mark templates to which
917 define_subst ATTR_NAME should be applied. This attribute is set and
918 defined implicitly and automatically. */
920 add_define_attr_for_define_subst (const char *attr_name
, vec
<rtx
> *queue
)
922 rtx const_str
, return_rtx
;
924 return_rtx
= rtx_alloc (DEFINE_ATTR
);
925 PUT_CODE (return_rtx
, DEFINE_ATTR
);
927 const_str
= rtx_alloc (CONST_STRING
);
928 PUT_CODE (const_str
, CONST_STRING
);
929 XSTR (const_str
, 0) = xstrdup ("no");
931 XSTR (return_rtx
, 0) = xstrdup (attr_name
);
932 XSTR (return_rtx
, 1) = xstrdup ("no,yes");
933 XEXP (return_rtx
, 2) = const_str
;
935 queue
->safe_push (return_rtx
);
938 /* This routine generates DEFINE_SUBST_ATTR expression with operands
939 ATTR_OPERANDS and places it to QUEUE. */
941 add_define_subst_attr (const char **attr_operands
, vec
<rtx
> *queue
)
946 return_rtx
= rtx_alloc (DEFINE_SUBST_ATTR
);
947 PUT_CODE (return_rtx
, DEFINE_SUBST_ATTR
);
949 for (i
= 0; i
< 4; i
++)
950 XSTR (return_rtx
, i
) = xstrdup (attr_operands
[i
]);
952 queue
->safe_push (return_rtx
);
955 /* Read define_subst_attribute construction. It has next form:
956 (define_subst_attribute <attribute_name> <iterator_name> <value1> <value2>)
957 Attribute is substituted with value1 when no subst is applied and with
958 value2 in the opposite case.
959 Attributes are added to SUBST_ATTRS_TABLE.
960 In case the iterator is encountered for the first time, it's added to
961 SUBST_ITERS_TABLE. Also, implicit define_attr is generated. */
964 read_subst_mapping (htab_t subst_iters_table
, htab_t subst_attrs_table
,
968 struct map_value
**end_ptr
;
969 const char *attr_operands
[4];
972 for (i
= 0; i
< 4; i
++)
973 attr_operands
[i
] = rtx_reader_ptr
->read_string (false);
975 add_define_subst_attr (attr_operands
, queue
);
977 bind_subst_iter_and_attr (attr_operands
[1], attr_operands
[0]);
979 m
= (struct mapping
*) htab_find (substs
.iterators
, &attr_operands
[1]);
982 m
= add_mapping (&substs
, subst_iters_table
, attr_operands
[1]);
983 end_ptr
= &m
->values
;
984 end_ptr
= add_map_value (end_ptr
, 1, "");
985 end_ptr
= add_map_value (end_ptr
, 2, "");
987 add_define_attr_for_define_subst (attr_operands
[1], queue
);
990 m
= add_mapping (&substs
, subst_attrs_table
, attr_operands
[0]);
991 end_ptr
= &m
->values
;
992 end_ptr
= add_map_value (end_ptr
, 1, attr_operands
[2]);
993 end_ptr
= add_map_value (end_ptr
, 2, attr_operands
[3]);
996 /* Check newly-created code iterator ITERATOR to see whether every code has the
1000 check_code_iterator (struct mapping
*iterator
)
1002 struct map_value
*v
;
1003 enum rtx_code bellwether
;
1005 bellwether
= (enum rtx_code
) iterator
->values
->number
;
1006 for (v
= iterator
->values
->next
; v
!= 0; v
= v
->next
)
1007 if (strcmp (GET_RTX_FORMAT (bellwether
), GET_RTX_FORMAT (v
->number
)) != 0)
1008 fatal_with_file_and_line ("code iterator `%s' combines "
1009 "different rtx formats", iterator
->name
);
1012 /* Read an rtx-related declaration from the MD file, given that it
1013 starts with directive name RTX_NAME. Return true if it expands to
1014 one or more rtxes (as defined by rtx.def). When returning true,
1015 store the list of rtxes as an EXPR_LIST in *X. */
1018 rtx_reader::read_rtx (const char *rtx_name
, vec
<rtx
> *rtxen
)
1020 static bool initialized
= false;
1022 /* Do one-time initialization. */
1025 initialize_iterators ();
1029 /* Handle various rtx-related declarations that aren't themselves
1030 encoded as rtxes. */
1031 if (strcmp (rtx_name
, "define_conditions") == 0)
1036 if (strcmp (rtx_name
, "define_mode_attr") == 0)
1038 read_mapping (&modes
, modes
.attrs
);
1041 if (strcmp (rtx_name
, "define_mode_iterator") == 0)
1043 read_mapping (&modes
, modes
.iterators
);
1046 if (strcmp (rtx_name
, "define_code_attr") == 0)
1048 read_mapping (&codes
, codes
.attrs
);
1051 if (strcmp (rtx_name
, "define_code_iterator") == 0)
1053 check_code_iterator (read_mapping (&codes
, codes
.iterators
));
1056 if (strcmp (rtx_name
, "define_int_attr") == 0)
1058 read_mapping (&ints
, ints
.attrs
);
1061 if (strcmp (rtx_name
, "define_int_iterator") == 0)
1063 read_mapping (&ints
, ints
.iterators
);
1066 if (strcmp (rtx_name
, "define_subst_attr") == 0)
1068 read_subst_mapping (substs
.iterators
, substs
.attrs
, rtxen
);
1070 /* READ_SUBST_MAPPING could generate a new DEFINE_ATTR. Return
1071 TRUE to process it. */
1075 apply_iterators (read_rtx_code (rtx_name
), rtxen
);
1076 iterator_uses
.truncate (0);
1077 attribute_uses
.truncate (0);
1082 /* Subroutine of read_rtx and read_nested_rtx. CODE_NAME is the name of
1083 either an rtx code or a code iterator. Parse the rest of the rtx and
1087 rtx_reader::read_rtx_code (const char *code_name
)
1090 struct mapping
*iterator
;
1091 const char *format_ptr
;
1092 struct md_name name
;
1096 /* Linked list structure for making RTXs: */
1099 struct rtx_list
*next
;
1100 rtx value
; /* Value of this node. */
1103 /* If this code is an iterator, build the rtx using the iterator's
1105 iterator
= (struct mapping
*) htab_find (codes
.iterators
, &code_name
);
1107 code
= (enum rtx_code
) iterator
->values
->number
;
1109 code
= (enum rtx_code
) codes
.find_builtin (code_name
);
1111 /* If we end up with an insn expression then we free this space below. */
1112 return_rtx
= rtx_alloc (code
);
1113 format_ptr
= GET_RTX_FORMAT (code
);
1114 memset (return_rtx
, 0, RTX_CODE_SIZE (code
));
1115 PUT_CODE (return_rtx
, code
);
1118 record_iterator_use (iterator
, return_rtx
);
1120 /* If what follows is `: mode ', read it and
1121 store the mode in the rtx. */
1123 c
= read_skip_spaces ();
1127 record_potential_iterator_use (&modes
, return_rtx
, name
.string
);
1132 for (int idx
= 0; format_ptr
[idx
] != 0; idx
++)
1133 read_rtx_operand (return_rtx
, idx
);
1135 if (CONST_WIDE_INT_P (return_rtx
))
1138 validate_const_wide_int (name
.string
);
1140 const char *s
= name
.string
;
1143 int gs
= HOST_BITS_PER_WIDE_INT
/4;
1145 char * buf
= XALLOCAVEC (char, gs
+ 1);
1146 unsigned HOST_WIDE_INT wi
;
1149 /* Skip the leading spaces. */
1150 while (*s
&& ISSPACE (*s
))
1153 /* Skip the leading 0x. */
1154 gcc_assert (s
[0] == '0');
1155 gcc_assert (s
[1] == 'x');
1160 wlen
= (len
+ gs
- 1) / gs
; /* Number of words needed */
1162 return_rtx
= const_wide_int_alloc (wlen
);
1166 #if HOST_BITS_PER_WIDE_INT == 64
1167 sscanf (s
+ pos
, "%16" HOST_WIDE_INT_PRINT
"x", &wi
);
1169 sscanf (s
+ pos
, "%8" HOST_WIDE_INT_PRINT
"x", &wi
);
1171 CWI_ELT (return_rtx
, index
++) = wi
;
1174 strncpy (buf
, s
, gs
- pos
);
1176 sscanf (buf
, "%" HOST_WIDE_INT_PRINT
"x", &wi
);
1177 CWI_ELT (return_rtx
, index
++) = wi
;
1178 /* TODO: After reading, do we want to canonicalize with:
1179 value = lookup_const_wide_int (value); ? */
1183 c
= read_skip_spaces ();
1184 /* Syntactic sugar for AND and IOR, allowing Lisp-like
1185 arbitrary number of arguments for them. */
1187 && (GET_CODE (return_rtx
) == AND
1188 || GET_CODE (return_rtx
) == IOR
))
1189 return read_rtx_variadic (return_rtx
);
1195 /* Subroutine of read_rtx_code. Parse operand IDX within RETURN_RTX,
1196 based on the corresponding format character within GET_RTX_FORMAT
1197 for the GET_CODE (RETURN_RTX). */
1200 rtx_reader::read_rtx_operand (rtx return_rtx
, int idx
)
1202 RTX_CODE code
= GET_CODE (return_rtx
);
1203 const char *format_ptr
= GET_RTX_FORMAT (code
);
1205 struct md_name name
;
1207 switch (format_ptr
[idx
])
1209 /* 0 means a field for internal use only.
1210 Don't expect it to be present in the input. */
1213 ORIGINAL_REGNO (return_rtx
) = REGNO (return_rtx
);
1218 XEXP (return_rtx
, idx
) = read_nested_rtx ();
1222 /* 'V' is an optional vector: if a closeparen follows,
1223 just store NULL for this element. */
1224 c
= read_skip_spaces ();
1228 XVEC (return_rtx
, idx
) = 0;
1231 /* Now process the vector. */
1236 /* Obstack to store scratch vector in. */
1237 struct obstack vector_stack
;
1238 int list_counter
= 0;
1239 rtvec return_vec
= NULL_RTVEC
;
1241 require_char_ws ('[');
1243 /* Add expressions to a list, while keeping a count. */
1244 obstack_init (&vector_stack
);
1245 while ((c
= read_skip_spaces ()) && c
!= ']')
1248 fatal_expected_char (']', c
);
1251 obstack_ptr_grow (&vector_stack
, read_nested_rtx ());
1253 if (list_counter
> 0)
1255 return_vec
= rtvec_alloc (list_counter
);
1256 memcpy (&return_vec
->elem
[0], obstack_finish (&vector_stack
),
1257 list_counter
* sizeof (rtx
));
1259 else if (format_ptr
[idx
] == 'E')
1260 fatal_with_file_and_line ("vector must have at least one element");
1261 XVEC (return_rtx
, idx
) = return_vec
;
1262 obstack_free (&vector_stack
, NULL
);
1263 /* close bracket gotten */
1274 c
= read_skip_spaces ();
1278 /* 'S' fields are optional and should be NULL if no string
1279 was given. Also allow normal 's' and 'T' strings to be
1280 omitted, treating them in the same way as empty strings. */
1281 XSTR (return_rtx
, idx
) = (format_ptr
[idx
] == 'S' ? NULL
: "");
1285 /* The output template slot of a DEFINE_INSN,
1286 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
1287 gets a star inserted as its first character, if it is
1288 written with a brace block instead of a string constant. */
1289 star_if_braced
= (format_ptr
[idx
] == 'T');
1291 stringbuf
= read_string (star_if_braced
);
1293 /* For insn patterns, we want to provide a default name
1294 based on the file and line, like "*foo.md:12", if the
1295 given name is blank. These are only for define_insn and
1296 define_insn_and_split, to aid debugging. */
1297 if (*stringbuf
== '\0'
1299 && (GET_CODE (return_rtx
) == DEFINE_INSN
1300 || GET_CODE (return_rtx
) == DEFINE_INSN_AND_SPLIT
))
1303 const char *read_md_filename
= get_filename ();
1304 const char *fn
= (read_md_filename
? read_md_filename
: "rtx");
1306 for (slash
= fn
; *slash
; slash
++)
1307 if (*slash
== '/' || *slash
== '\\' || *slash
== ':')
1309 obstack_1grow (&m_string_obstack
, '*');
1310 obstack_grow (&m_string_obstack
, fn
, strlen (fn
));
1311 sprintf (line_name
, ":%d", get_lineno ());
1312 obstack_grow (&m_string_obstack
, line_name
, strlen (line_name
)+1);
1313 stringbuf
= XOBFINISH (&m_string_obstack
, char *);
1316 /* Find attr-names in the string. */
1318 char *start
, *end
, *ptr
;
1322 while ((start
= strchr (end
, '<')) && (end
= strchr (start
, '>')))
1324 if ((end
- start
- 1 > 0)
1325 && (end
- start
- 1 < (int)sizeof (tmpstr
)))
1327 strncpy (tmpstr
, start
+1, end
-start
-1);
1328 tmpstr
[end
-start
-1] = 0;
1334 = (struct mapping
*) htab_find (substs
.attrs
, &ptr
);
1337 /* Here we should find linked subst-iter. */
1338 str
= find_subst_iter_by_attr (ptr
);
1340 m
= (struct mapping
*) htab_find (substs
.iterators
, &str
);
1345 record_iterator_use (m
, return_rtx
);
1349 XTMPL (return_rtx
, idx
) = stringbuf
;
1351 XSTR (return_rtx
, idx
) = stringbuf
;
1357 HOST_WIDE_INT tmp_wide
;
1359 validate_const_int (name
.string
);
1360 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1361 tmp_wide
= atoi (name
.string
);
1363 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1364 tmp_wide
= atol (name
.string
);
1366 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1367 But prefer not to use our hand-rolled function above either. */
1368 #if HAVE_DECL_ATOLL || !defined(HAVE_ATOQ)
1369 tmp_wide
= atoll (name
.string
);
1371 tmp_wide
= atoq (name
.string
);
1375 XWINT (return_rtx
, idx
) = tmp_wide
;
1381 /* Can be an iterator or an integer constant. */
1383 record_potential_iterator_use (&ints
, &XINT (return_rtx
, idx
),
1389 validate_const_int (name
.string
);
1390 set_regno_raw (return_rtx
, atoi (name
.string
), 1);
1391 REG_ATTRS (return_rtx
) = NULL
;
1399 /* Read a nested rtx construct from the MD file and return it. */
1402 rtx_reader::read_nested_rtx ()
1404 struct md_name name
;
1407 require_char_ws ('(');
1410 if (strcmp (name
.string
, "nil") == 0)
1413 return_rtx
= read_rtx_code (name
.string
);
1415 require_char_ws (')');
1420 /* Mutually recursive subroutine of read_rtx which reads
1421 (thing x1 x2 x3 ...) and produces RTL as if
1422 (thing x1 (thing x2 (thing x3 ...))) had been written.
1423 When called, FORM is (thing x1 x2), and the file position
1424 is just past the leading parenthesis of x3. Only works
1425 for THINGs which are dyadic expressions, e.g. AND, IOR. */
1427 rtx_reader::read_rtx_variadic (rtx form
)
1436 q
= rtx_alloc (GET_CODE (p
));
1437 PUT_MODE (q
, GET_MODE (p
));
1439 XEXP (q
, 0) = XEXP (p
, 1);
1440 XEXP (q
, 1) = read_nested_rtx ();
1444 c
= read_skip_spaces ();