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 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 XINT (x
, index
) = value
;
228 #ifdef GENERATOR_FILE
230 /* This routine adds attribute or does nothing depending on VALUE. When
231 VALUE is 1, it does nothing - the first duplicate of original
232 template is kept untouched when it's subjected to a define_subst.
233 When VALUE isn't 1, the routine modifies RTL-template RT, adding
234 attribute, named exactly as define_subst, which later will be
235 applied. If such attribute has already been added, then no the
236 routine has no effect. */
238 apply_subst_iterator (rtx rt
, unsigned int, int value
)
241 rtvec attrs_vec
, new_attrs_vec
;
245 gcc_assert (GET_CODE (rt
) == DEFINE_INSN
246 || GET_CODE (rt
) == DEFINE_EXPAND
);
248 attrs_vec
= XVEC (rt
, 4);
250 /* If we've already added attribute 'current_iterator_name', then we
251 have nothing to do now. */
254 for (i
= 0; i
< GET_NUM_ELEM (attrs_vec
); i
++)
256 if (strcmp (XSTR (attrs_vec
->elem
[i
], 0), current_iterator_name
) == 0)
261 /* Add attribute with subst name - it serves as a mark for
262 define_subst which later would be applied to this pattern. */
263 new_attr
= rtx_alloc (SET_ATTR
);
264 PUT_CODE (new_attr
, SET_ATTR
);
265 XSTR (new_attr
, 0) = xstrdup (current_iterator_name
);
266 XSTR (new_attr
, 1) = xstrdup ("yes");
270 new_attrs_vec
= rtvec_alloc (1);
271 new_attrs_vec
->elem
[0] = new_attr
;
275 new_attrs_vec
= rtvec_alloc (GET_NUM_ELEM (attrs_vec
) + 1);
276 memcpy (&new_attrs_vec
->elem
[0], &attrs_vec
->elem
[0],
277 GET_NUM_ELEM (attrs_vec
) * sizeof (rtx
));
278 new_attrs_vec
->elem
[GET_NUM_ELEM (attrs_vec
)] = new_attr
;
280 XVEC (rt
, 4) = new_attrs_vec
;
283 /* Map subst-attribute ATTR to subst iterator ITER. */
286 bind_subst_iter_and_attr (const char *iter
, const char *attr
)
288 struct subst_attr_to_iter_mapping
*value
;
290 if (!subst_attr_to_iter_map
)
291 subst_attr_to_iter_map
=
292 htab_create (1, leading_string_hash
, leading_string_eq_p
, 0);
293 value
= XNEW (struct subst_attr_to_iter_mapping
);
294 value
->attr_name
= xstrdup (attr
);
295 value
->iter_name
= xstrdup (iter
);
296 slot
= htab_find_slot (subst_attr_to_iter_map
, value
, INSERT
);
300 #endif /* #ifdef GENERATOR_FILE */
302 /* Return name of a subst-iterator, corresponding to subst-attribute ATTR. */
305 find_subst_iter_by_attr (const char *attr
)
307 char *iter_name
= NULL
;
308 struct subst_attr_to_iter_mapping
*value
;
309 value
= (struct subst_attr_to_iter_mapping
*)
310 htab_find (subst_attr_to_iter_map
, &attr
);
312 iter_name
= value
->iter_name
;
316 /* Map attribute string P to its current value. Return null if the attribute
319 static struct map_value
*
320 map_attr_string (const char *p
)
323 struct mapping
*iterator
;
327 int iterator_name_len
;
329 /* Peel off any "iterator:" prefix. Set ATTR to the start of the
331 attr
= strchr (p
, ':');
334 iterator_name_len
= -1;
339 iterator_name_len
= attr
- p
;
343 FOR_EACH_VEC_ELT (current_iterators
, i
, iterator
)
345 /* If an iterator name was specified, check that it matches. */
346 if (iterator_name_len
>= 0
347 && (strncmp (p
, iterator
->name
, iterator_name_len
) != 0
348 || iterator
->name
[iterator_name_len
] != 0))
351 /* Find the attribute specification. */
352 m
= (struct mapping
*) htab_find (iterator
->group
->attrs
, &attr
);
355 /* In contrast to code/mode/int iterators, attributes of subst
356 iterators are linked to one specific subst-iterator. So, if
357 we are dealing with subst-iterator, we should check if it's
358 the one which linked with the given attribute. */
359 if (iterator
->group
== &substs
)
361 char *iter_name
= find_subst_iter_by_attr (attr
);
362 if (strcmp (iter_name
, iterator
->name
) != 0)
365 /* Find the attribute value associated with the current
367 for (v
= m
->values
; v
; v
= v
->next
)
368 if (v
->number
== iterator
->current_value
->number
)
375 /* Apply the current iterator values to STRING. Return the new string
376 if any changes were needed, otherwise return STRING itself. */
379 md_reader::apply_iterator_to_string (const char *string
)
381 char *base
, *copy
, *p
, *start
, *end
;
387 base
= p
= copy
= ASTRDUP (string
);
388 while ((start
= strchr (p
, '<')) && (end
= strchr (start
, '>')))
393 v
= map_attr_string (p
);
398 /* Add everything between the last copied byte and the '<',
399 then add in the attribute value. */
400 obstack_grow (&m_string_obstack
, base
, start
- base
);
401 obstack_grow (&m_string_obstack
, v
->string
, strlen (v
->string
));
406 obstack_grow (&m_string_obstack
, base
, strlen (base
) + 1);
407 copy
= XOBFINISH (&m_string_obstack
, char *);
408 copy_md_ptr_loc (copy
, string
);
414 /* Return a deep copy of X, substituting the current iterator
415 values into any strings. */
418 md_reader::copy_rtx_for_iterators (rtx original
)
420 const char *format_ptr
, *p
;
427 /* Create a shallow copy of ORIGINAL. */
428 x
= rtx_alloc (GET_CODE (original
));
429 memcpy (x
, original
, RTX_CODE_SIZE (GET_CODE (original
)));
431 /* Change each string and recursively change each rtx. */
432 format_ptr
= GET_RTX_FORMAT (GET_CODE (original
));
433 for (i
= 0; format_ptr
[i
] != 0; i
++)
434 switch (format_ptr
[i
])
437 while (XTMPL (x
, i
) != (p
= apply_iterator_to_string (XTMPL (x
, i
))))
443 while (XSTR (x
, i
) != (p
= apply_iterator_to_string (XSTR (x
, i
))))
448 XEXP (x
, i
) = copy_rtx_for_iterators (XEXP (x
, i
));
453 if (XVEC (original
, i
))
455 XVEC (x
, i
) = rtvec_alloc (XVECLEN (original
, i
));
456 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
458 = copy_rtx_for_iterators (XVECEXP (original
, i
, j
));
468 #ifdef GENERATOR_FILE
470 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
471 has the form "&& ..." (as used in define_insn_and_splits), assume that
472 EXTRA is already satisfied. Empty strings are treated like "true". */
475 add_condition_to_string (const char *original
, const char *extra
)
477 if (original
!= 0 && original
[0] == '&' && original
[1] == '&')
479 return rtx_reader_ptr
->join_c_conditions (original
, extra
);
482 /* Like add_condition, but applied to all conditions in rtx X. */
485 add_condition_to_rtx (rtx x
, const char *extra
)
487 switch (GET_CODE (x
))
492 XSTR (x
, 2) = add_condition_to_string (XSTR (x
, 2), extra
);
496 case DEFINE_PEEPHOLE
:
497 case DEFINE_PEEPHOLE2
:
498 case DEFINE_COND_EXEC
:
499 XSTR (x
, 1) = add_condition_to_string (XSTR (x
, 1), extra
);
502 case DEFINE_INSN_AND_SPLIT
:
503 XSTR (x
, 2) = add_condition_to_string (XSTR (x
, 2), extra
);
504 XSTR (x
, 4) = add_condition_to_string (XSTR (x
, 4), extra
);
512 /* Apply the current iterator values to all attribute_uses. */
515 apply_attribute_uses (void)
521 FOR_EACH_VEC_ELT (attribute_uses
, i
, ause
)
523 v
= map_attr_string (ause
->value
);
525 fatal_with_file_and_line ("unknown iterator value `%s'", ause
->value
);
526 ause
->group
->apply_iterator (ause
->x
, ause
->index
,
527 ause
->group
->find_builtin (v
->string
));
531 /* A htab_traverse callback for iterators. Add all used iterators
532 to current_iterators. */
535 add_current_iterators (void **slot
, void *data ATTRIBUTE_UNUSED
)
537 struct mapping
*iterator
;
539 iterator
= (struct mapping
*) *slot
;
540 if (iterator
->current_value
)
541 current_iterators
.safe_push (iterator
);
545 /* Expand all iterators in the current rtx, which is given as ORIGINAL.
546 Build a list of expanded rtxes in the EXPR_LIST pointed to by QUEUE. */
549 apply_iterators (rtx original
, vec
<rtx
> *queue
)
552 const char *condition
;
554 struct mapping
*iterator
;
558 if (iterator_uses
.is_empty ())
560 /* Raise an error if any attributes were used. */
561 apply_attribute_uses ();
562 queue
->safe_push (original
);
566 /* Clear out the iterators from the previous run. */
567 FOR_EACH_VEC_ELT (current_iterators
, i
, iterator
)
568 iterator
->current_value
= NULL
;
569 current_iterators
.truncate (0);
571 /* Mark the iterators that we need this time. */
572 FOR_EACH_VEC_ELT (iterator_uses
, i
, iuse
)
573 iuse
->iterator
->current_value
= iuse
->iterator
->values
;
575 /* Get the list of iterators that are in use, preserving the
576 definition order within each group. */
577 htab_traverse (modes
.iterators
, add_current_iterators
, NULL
);
578 htab_traverse (codes
.iterators
, add_current_iterators
, NULL
);
579 htab_traverse (ints
.iterators
, add_current_iterators
, NULL
);
580 htab_traverse (substs
.iterators
, add_current_iterators
, NULL
);
581 gcc_assert (!current_iterators
.is_empty ());
585 /* Apply the current iterator values. Accumulate a condition to
586 say when the resulting rtx can be used. */
588 FOR_EACH_VEC_ELT (iterator_uses
, i
, iuse
)
590 if (iuse
->iterator
->group
== &substs
)
592 v
= iuse
->iterator
->current_value
;
593 iuse
->iterator
->group
->apply_iterator (iuse
->x
, iuse
->index
,
595 condition
= rtx_reader_ptr
->join_c_conditions (condition
, v
->string
);
597 apply_attribute_uses ();
598 x
= rtx_reader_ptr
->copy_rtx_for_iterators (original
);
599 add_condition_to_rtx (x
, condition
);
601 /* We apply subst iterator after RTL-template is copied, as during
602 subst-iterator processing, we could add an attribute to the
603 RTL-template, and we don't want to do it in the original one. */
604 FOR_EACH_VEC_ELT (iterator_uses
, i
, iuse
)
606 v
= iuse
->iterator
->current_value
;
607 if (iuse
->iterator
->group
== &substs
)
611 current_iterator_name
= iuse
->iterator
->name
;
612 iuse
->iterator
->group
->apply_iterator (iuse
->x
, iuse
->index
,
616 /* Add the new rtx to the end of the queue. */
617 queue
->safe_push (x
);
619 /* Lexicographically increment the iterator value sequence.
620 That is, cycle through iterator values, starting from the right,
621 and stopping when one of them doesn't wrap around. */
622 i
= current_iterators
.length ();
628 iterator
= current_iterators
[i
];
629 iterator
->current_value
= iterator
->current_value
->next
;
630 if (iterator
->current_value
)
632 iterator
->current_value
= iterator
->values
;
636 #endif /* #ifdef GENERATOR_FILE */
638 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
639 of the mapping and GROUP is the group to which it belongs. */
641 static struct mapping
*
642 add_mapping (struct iterator_group
*group
, htab_t table
, const char *name
)
647 m
= XNEW (struct mapping
);
648 m
->name
= xstrdup (name
);
651 m
->current_value
= NULL
;
653 slot
= htab_find_slot (table
, m
, INSERT
);
655 fatal_with_file_and_line ("`%s' already defined", name
);
661 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
662 END_PTR points to the current null terminator for the list; return
663 a pointer the new null terminator. */
665 static struct map_value
**
666 add_map_value (struct map_value
**end_ptr
, int number
, const char *string
)
668 struct map_value
*value
;
670 value
= XNEW (struct map_value
);
672 value
->number
= number
;
673 value
->string
= string
;
679 /* Do one-time initialization of the mode and code attributes. */
682 initialize_iterators (void)
684 struct mapping
*lower
, *upper
;
685 struct map_value
**lower_ptr
, **upper_ptr
;
689 modes
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
690 modes
.iterators
= htab_create (13, leading_string_hash
,
691 leading_string_eq_p
, 0);
692 modes
.find_builtin
= find_mode
;
693 modes
.apply_iterator
= apply_mode_iterator
;
695 codes
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
696 codes
.iterators
= htab_create (13, leading_string_hash
,
697 leading_string_eq_p
, 0);
698 codes
.find_builtin
= find_code
;
699 codes
.apply_iterator
= apply_code_iterator
;
701 ints
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
702 ints
.iterators
= htab_create (13, leading_string_hash
,
703 leading_string_eq_p
, 0);
704 ints
.find_builtin
= find_int
;
705 ints
.apply_iterator
= apply_int_iterator
;
707 substs
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
708 substs
.iterators
= htab_create (13, leading_string_hash
,
709 leading_string_eq_p
, 0);
710 substs
.find_builtin
= find_int
; /* We don't use it, anyway. */
711 #ifdef GENERATOR_FILE
712 substs
.apply_iterator
= apply_subst_iterator
;
715 lower
= add_mapping (&modes
, modes
.attrs
, "mode");
716 upper
= add_mapping (&modes
, modes
.attrs
, "MODE");
717 lower_ptr
= &lower
->values
;
718 upper_ptr
= &upper
->values
;
719 for (i
= 0; i
< MAX_MACHINE_MODE
; i
++)
721 copy
= xstrdup (GET_MODE_NAME (i
));
722 for (p
= copy
; *p
!= 0; p
++)
725 upper_ptr
= add_map_value (upper_ptr
, i
, GET_MODE_NAME (i
));
726 lower_ptr
= add_map_value (lower_ptr
, i
, copy
);
729 lower
= add_mapping (&codes
, codes
.attrs
, "code");
730 upper
= add_mapping (&codes
, codes
.attrs
, "CODE");
731 lower_ptr
= &lower
->values
;
732 upper_ptr
= &upper
->values
;
733 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
735 copy
= xstrdup (GET_RTX_NAME (i
));
736 for (p
= copy
; *p
!= 0; p
++)
739 lower_ptr
= add_map_value (lower_ptr
, i
, GET_RTX_NAME (i
));
740 upper_ptr
= add_map_value (upper_ptr
, i
, copy
);
744 /* Provide a version of a function to read a long long if the system does
746 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !HAVE_DECL_ATOLL && !defined(HAVE_ATOQ)
747 HOST_WIDE_INT
atoll (const char *);
750 atoll (const char *p
)
753 HOST_WIDE_INT tmp_wide
;
765 HOST_WIDE_INT new_wide
= tmp_wide
*10 + (*p
- '0');
766 if (new_wide
< tmp_wide
)
768 /* Return INT_MAX equiv on overflow. */
769 tmp_wide
= HOST_WIDE_INT_M1U
>> 1;
777 tmp_wide
= -tmp_wide
;
783 #ifdef GENERATOR_FILE
784 /* Process a define_conditions directive, starting with the optional
785 space after the "define_conditions". The directive looks like this:
793 It's not intended to appear in machine descriptions. It is
794 generated by (the program generated by) genconditions.c, and
795 slipped in at the beginning of the sequence of MD files read by
796 most of the other generators. */
798 md_reader::read_conditions ()
802 require_char_ws ('[');
804 while ( (c
= read_skip_spaces ()) != ']')
811 fatal_expected_char ('(', c
);
814 validate_const_int (name
.string
);
815 value
= atoi (name
.string
);
817 require_char_ws ('"');
818 expr
= read_quoted_string ();
820 require_char_ws (')');
822 add_c_test (expr
, value
);
825 #endif /* #ifdef GENERATOR_FILE */
828 validate_const_int (const char *string
)
834 while (*cp
&& ISSPACE (*cp
))
836 if (*cp
== '-' || *cp
== '+')
847 fatal_with_file_and_line ("invalid decimal constant \"%s\"\n", string
);
851 validate_const_wide_int (const char *string
)
857 while (*cp
&& ISSPACE (*cp
))
859 /* Skip the leading 0x. */
860 if (cp
[0] == '0' || cp
[1] == 'x')
867 if (! ISXDIGIT (*cp
))
870 fatal_with_file_and_line ("invalid hex constant \"%s\"\n", string
);
873 /* Record that X uses iterator ITERATOR. If the use is in an operand
874 of X, INDEX is the index of that operand, otherwise it is ignored. */
877 record_iterator_use (struct mapping
*iterator
, rtx x
, unsigned int index
)
879 struct iterator_use iuse
= {iterator
, x
, index
};
880 iterator_uses
.safe_push (iuse
);
883 /* Record that X uses attribute VALUE, which must match a built-in
884 value from group GROUP. If the use is in an operand of X, INDEX
885 is the index of that operand, otherwise it is ignored. */
888 record_attribute_use (struct iterator_group
*group
, rtx x
,
889 unsigned int index
, const char *value
)
891 struct attribute_use ause
= {group
, value
, x
, index
};
892 attribute_uses
.safe_push (ause
);
895 /* Interpret NAME as either a built-in value, iterator or attribute
896 for group GROUP. X and INDEX are the values to pass to GROUP's
897 apply_iterator callback. */
900 md_reader::record_potential_iterator_use (struct iterator_group
*group
,
901 rtx x
, unsigned int index
,
908 if (name
[0] == '<' && name
[len
- 1] == '>')
910 /* Copy the attribute string into permanent storage, without the
911 angle brackets around it. */
912 obstack_grow0 (&m_string_obstack
, name
+ 1, len
- 2);
913 record_attribute_use (group
, x
, index
,
914 XOBFINISH (&m_string_obstack
, char *));
918 m
= (struct mapping
*) htab_find (group
->iterators
, &name
);
920 record_iterator_use (m
, x
, index
);
922 group
->apply_iterator (x
, index
, group
->find_builtin (name
));
926 #ifdef GENERATOR_FILE
928 /* Finish reading a declaration of the form:
930 (define... <name> [<value1> ... <valuen>])
932 from the MD file, where each <valuei> is either a bare symbol name or a
933 "(<name> <string>)" pair. The "(define..." part has already been read.
935 Represent the declaration as a "mapping" structure; add it to TABLE
936 (which belongs to GROUP) and return it. */
939 md_reader::read_mapping (struct iterator_group
*group
, htab_t table
)
943 struct map_value
**end_ptr
;
947 /* Read the mapping name and create a structure for it. */
949 m
= add_mapping (group
, table
, name
.string
);
951 require_char_ws ('[');
953 /* Read each value. */
954 end_ptr
= &m
->values
;
955 c
= read_skip_spaces ();
960 /* A bare symbol name that is implicitly paired to an
968 /* A "(name string)" pair. */
970 string
= read_string (false);
971 require_char_ws (')');
973 number
= group
->find_builtin (name
.string
);
974 end_ptr
= add_map_value (end_ptr
, number
, string
);
975 c
= read_skip_spaces ();
982 /* For iterator with name ATTR_NAME generate define_attr with values
983 'yes' and 'no'. This attribute is used to mark templates to which
984 define_subst ATTR_NAME should be applied. This attribute is set and
985 defined implicitly and automatically. */
987 add_define_attr_for_define_subst (const char *attr_name
, vec
<rtx
> *queue
)
989 rtx const_str
, return_rtx
;
991 return_rtx
= rtx_alloc (DEFINE_ATTR
);
992 PUT_CODE (return_rtx
, DEFINE_ATTR
);
994 const_str
= rtx_alloc (CONST_STRING
);
995 PUT_CODE (const_str
, CONST_STRING
);
996 XSTR (const_str
, 0) = xstrdup ("no");
998 XSTR (return_rtx
, 0) = xstrdup (attr_name
);
999 XSTR (return_rtx
, 1) = xstrdup ("no,yes");
1000 XEXP (return_rtx
, 2) = const_str
;
1002 queue
->safe_push (return_rtx
);
1005 /* This routine generates DEFINE_SUBST_ATTR expression with operands
1006 ATTR_OPERANDS and places it to QUEUE. */
1008 add_define_subst_attr (const char **attr_operands
, vec
<rtx
> *queue
)
1013 return_rtx
= rtx_alloc (DEFINE_SUBST_ATTR
);
1014 PUT_CODE (return_rtx
, DEFINE_SUBST_ATTR
);
1016 for (i
= 0; i
< 4; i
++)
1017 XSTR (return_rtx
, i
) = xstrdup (attr_operands
[i
]);
1019 queue
->safe_push (return_rtx
);
1022 /* Read define_subst_attribute construction. It has next form:
1023 (define_subst_attribute <attribute_name> <iterator_name> <value1> <value2>)
1024 Attribute is substituted with value1 when no subst is applied and with
1025 value2 in the opposite case.
1026 Attributes are added to SUBST_ATTRS_TABLE.
1027 In case the iterator is encountered for the first time, it's added to
1028 SUBST_ITERS_TABLE. Also, implicit define_attr is generated. */
1031 read_subst_mapping (htab_t subst_iters_table
, htab_t subst_attrs_table
,
1035 struct map_value
**end_ptr
;
1036 const char *attr_operands
[4];
1039 for (i
= 0; i
< 4; i
++)
1040 attr_operands
[i
] = rtx_reader_ptr
->read_string (false);
1042 add_define_subst_attr (attr_operands
, queue
);
1044 bind_subst_iter_and_attr (attr_operands
[1], attr_operands
[0]);
1046 m
= (struct mapping
*) htab_find (substs
.iterators
, &attr_operands
[1]);
1049 m
= add_mapping (&substs
, subst_iters_table
, attr_operands
[1]);
1050 end_ptr
= &m
->values
;
1051 end_ptr
= add_map_value (end_ptr
, 1, "");
1052 end_ptr
= add_map_value (end_ptr
, 2, "");
1054 add_define_attr_for_define_subst (attr_operands
[1], queue
);
1057 m
= add_mapping (&substs
, subst_attrs_table
, attr_operands
[0]);
1058 end_ptr
= &m
->values
;
1059 end_ptr
= add_map_value (end_ptr
, 1, attr_operands
[2]);
1060 end_ptr
= add_map_value (end_ptr
, 2, attr_operands
[3]);
1063 /* Check newly-created code iterator ITERATOR to see whether every code has the
1067 check_code_iterator (struct mapping
*iterator
)
1069 struct map_value
*v
;
1070 enum rtx_code bellwether
;
1072 bellwether
= (enum rtx_code
) iterator
->values
->number
;
1073 for (v
= iterator
->values
->next
; v
!= 0; v
= v
->next
)
1074 if (strcmp (GET_RTX_FORMAT (bellwether
), GET_RTX_FORMAT (v
->number
)) != 0)
1075 fatal_with_file_and_line ("code iterator `%s' combines "
1076 "different rtx formats", iterator
->name
);
1079 /* Read an rtx-related declaration from the MD file, given that it
1080 starts with directive name RTX_NAME. Return true if it expands to
1081 one or more rtxes (as defined by rtx.def). When returning true,
1082 store the list of rtxes as an EXPR_LIST in *X. */
1085 rtx_reader::read_rtx (const char *rtx_name
, vec
<rtx
> *rtxen
)
1087 /* Handle various rtx-related declarations that aren't themselves
1088 encoded as rtxes. */
1089 if (strcmp (rtx_name
, "define_conditions") == 0)
1094 if (strcmp (rtx_name
, "define_mode_attr") == 0)
1096 read_mapping (&modes
, modes
.attrs
);
1099 if (strcmp (rtx_name
, "define_mode_iterator") == 0)
1101 read_mapping (&modes
, modes
.iterators
);
1104 if (strcmp (rtx_name
, "define_code_attr") == 0)
1106 read_mapping (&codes
, codes
.attrs
);
1109 if (strcmp (rtx_name
, "define_code_iterator") == 0)
1111 check_code_iterator (read_mapping (&codes
, codes
.iterators
));
1114 if (strcmp (rtx_name
, "define_int_attr") == 0)
1116 read_mapping (&ints
, ints
.attrs
);
1119 if (strcmp (rtx_name
, "define_int_iterator") == 0)
1121 read_mapping (&ints
, ints
.iterators
);
1124 if (strcmp (rtx_name
, "define_subst_attr") == 0)
1126 read_subst_mapping (substs
.iterators
, substs
.attrs
, rtxen
);
1128 /* READ_SUBST_MAPPING could generate a new DEFINE_ATTR. Return
1129 TRUE to process it. */
1133 apply_iterators (rtx_reader_ptr
->read_rtx_code (rtx_name
), rtxen
);
1134 iterator_uses
.truncate (0);
1135 attribute_uses
.truncate (0);
1140 #endif /* #ifdef GENERATOR_FILE */
1142 /* Do one-time initialization. */
1145 one_time_initialization (void)
1147 static bool initialized
= false;
1151 initialize_iterators ();
1156 /* Consume characters until encountering a character in TERMINATOR_CHARS,
1157 consuming the terminator character if CONSUME_TERMINATOR is true.
1158 Return all characters before the terminator as an allocated buffer. */
1161 rtx_reader::read_until (const char *terminator_chars
, bool consume_terminator
)
1163 int ch
= read_skip_spaces ();
1169 if (strchr (terminator_chars
, ch
))
1171 if (!consume_terminator
)
1177 buf
.safe_push ('\0');
1178 return xstrdup (buf
.address ());
1181 /* Subroutine of read_rtx_code, for parsing zero or more flags. */
1184 read_flags (rtx return_rtx
)
1188 int ch
= read_char ();
1195 int flag_char
= read_char ();
1199 RTX_FLAG (return_rtx
, in_struct
) = 1;
1202 RTX_FLAG (return_rtx
, volatil
) = 1;
1205 RTX_FLAG (return_rtx
, unchanging
) = 1;
1208 RTX_FLAG (return_rtx
, frame_related
) = 1;
1211 RTX_FLAG (return_rtx
, jump
) = 1;
1214 RTX_FLAG (return_rtx
, call
) = 1;
1217 RTX_FLAG (return_rtx
, return_val
) = 1;
1220 fatal_with_file_and_line ("unrecognized flag: `%c'", flag_char
);
1225 /* Return the numeric value n for GET_REG_NOTE_NAME (n) for STRING,
1226 or fail if STRING isn't recognized. */
1229 parse_reg_note_name (const char *string
)
1231 for (int i
= 0; i
< REG_NOTE_MAX
; i
++)
1232 if (0 == strcmp (string
, GET_REG_NOTE_NAME (i
)))
1234 fatal_with_file_and_line ("unrecognized REG_NOTE name: `%s'", string
);
1237 /* Subroutine of read_rtx and read_nested_rtx. CODE_NAME is the name of
1238 either an rtx code or a code iterator. Parse the rest of the rtx and
1242 rtx_reader::read_rtx_code (const char *code_name
)
1245 struct mapping
*iterator
= NULL
;
1246 const char *format_ptr
;
1247 struct md_name name
;
1252 /* Linked list structure for making RTXs: */
1255 struct rtx_list
*next
;
1256 rtx value
; /* Value of this node. */
1259 /* Handle reuse_rtx ids e.g. "(0|scratch:DI)". */
1260 if (ISDIGIT (code_name
[0]))
1262 reuse_id
= atoi (code_name
);
1263 while (char ch
= *code_name
++)
1268 /* Handle "reuse_rtx". */
1269 if (strcmp (code_name
, "reuse_rtx") == 0)
1272 unsigned idx
= atoi (name
.string
);
1273 /* Look it up by ID. */
1274 gcc_assert (idx
< m_reuse_rtx_by_id
.length ());
1275 return_rtx
= m_reuse_rtx_by_id
[idx
];
1279 /* If this code is an iterator, build the rtx using the iterator's
1281 #ifdef GENERATOR_FILE
1282 iterator
= (struct mapping
*) htab_find (codes
.iterators
, &code_name
);
1284 code
= (enum rtx_code
) iterator
->values
->number
;
1286 code
= (enum rtx_code
) codes
.find_builtin (code_name
);
1288 code
= (enum rtx_code
) codes
.find_builtin (code_name
);
1291 /* If we end up with an insn expression then we free this space below. */
1292 return_rtx
= rtx_alloc (code
);
1293 format_ptr
= GET_RTX_FORMAT (code
);
1294 memset (return_rtx
, 0, RTX_CODE_SIZE (code
));
1295 PUT_CODE (return_rtx
, code
);
1299 /* Store away for later reuse. */
1300 m_reuse_rtx_by_id
.safe_grow_cleared (reuse_id
+ 1);
1301 m_reuse_rtx_by_id
[reuse_id
] = return_rtx
;
1305 record_iterator_use (iterator
, return_rtx
, 0);
1307 /* Check for flags. */
1308 read_flags (return_rtx
);
1310 /* Read REG_NOTE names for EXPR_LIST and INSN_LIST. */
1311 if ((GET_CODE (return_rtx
) == EXPR_LIST
1312 || GET_CODE (return_rtx
) == INSN_LIST
1313 || GET_CODE (return_rtx
) == INT_LIST
)
1314 && !m_in_call_function_usage
)
1316 char ch
= read_char ();
1320 PUT_MODE_RAW (return_rtx
,
1321 (machine_mode
)parse_reg_note_name (name
.string
));
1327 /* If what follows is `: mode ', read it and
1328 store the mode in the rtx. */
1330 c
= read_skip_spaces ();
1334 record_potential_iterator_use (&modes
, return_rtx
, 0, name
.string
);
1339 if (INSN_CHAIN_CODE_P (code
))
1342 INSN_UID (return_rtx
) = atoi (name
.string
);
1345 /* Use the format_ptr to parse the various operands of this rtx. */
1346 for (int idx
= 0; format_ptr
[idx
] != 0; idx
++)
1347 return_rtx
= read_rtx_operand (return_rtx
, idx
);
1349 /* Handle any additional information that after the regular fields
1350 (e.g. when parsing function dumps). */
1351 handle_any_trailing_information (return_rtx
);
1353 if (CONST_WIDE_INT_P (return_rtx
))
1356 validate_const_wide_int (name
.string
);
1358 const char *s
= name
.string
;
1361 int gs
= HOST_BITS_PER_WIDE_INT
/4;
1363 char * buf
= XALLOCAVEC (char, gs
+ 1);
1364 unsigned HOST_WIDE_INT wi
;
1367 /* Skip the leading spaces. */
1368 while (*s
&& ISSPACE (*s
))
1371 /* Skip the leading 0x. */
1372 gcc_assert (s
[0] == '0');
1373 gcc_assert (s
[1] == 'x');
1378 wlen
= (len
+ gs
- 1) / gs
; /* Number of words needed */
1380 return_rtx
= const_wide_int_alloc (wlen
);
1384 #if HOST_BITS_PER_WIDE_INT == 64
1385 sscanf (s
+ pos
, "%16" HOST_WIDE_INT_PRINT
"x", &wi
);
1387 sscanf (s
+ pos
, "%8" HOST_WIDE_INT_PRINT
"x", &wi
);
1389 CWI_ELT (return_rtx
, index
++) = wi
;
1392 strncpy (buf
, s
, gs
- pos
);
1394 sscanf (buf
, "%" HOST_WIDE_INT_PRINT
"x", &wi
);
1395 CWI_ELT (return_rtx
, index
++) = wi
;
1396 /* TODO: After reading, do we want to canonicalize with:
1397 value = lookup_const_wide_int (value); ? */
1401 c
= read_skip_spaces ();
1402 /* Syntactic sugar for AND and IOR, allowing Lisp-like
1403 arbitrary number of arguments for them. */
1405 && (GET_CODE (return_rtx
) == AND
1406 || GET_CODE (return_rtx
) == IOR
))
1407 return read_rtx_variadic (return_rtx
);
1413 /* Subroutine of read_rtx_code. Parse operand IDX within RETURN_RTX,
1414 based on the corresponding format character within GET_RTX_FORMAT
1415 for the GET_CODE (RETURN_RTX), and return RETURN_RTX.
1416 This is a virtual function, so that function_reader can override
1417 some parsing, and potentially return a different rtx. */
1420 rtx_reader::read_rtx_operand (rtx return_rtx
, int idx
)
1422 RTX_CODE code
= GET_CODE (return_rtx
);
1423 const char *format_ptr
= GET_RTX_FORMAT (code
);
1425 struct md_name name
;
1427 switch (format_ptr
[idx
])
1429 /* 0 means a field for internal use only.
1430 Don't expect it to be present in the input. */
1433 ORIGINAL_REGNO (return_rtx
) = REGNO (return_rtx
);
1437 XEXP (return_rtx
, idx
) = read_nested_rtx ();
1441 XEXP (return_rtx
, idx
) = read_nested_rtx ();
1445 /* 'V' is an optional vector: if a closeparen follows,
1446 just store NULL for this element. */
1447 c
= read_skip_spaces ();
1451 XVEC (return_rtx
, idx
) = 0;
1454 /* Now process the vector. */
1459 /* Obstack to store scratch vector in. */
1460 struct obstack vector_stack
;
1461 int list_counter
= 0;
1462 rtvec return_vec
= NULL_RTVEC
;
1464 require_char_ws ('[');
1466 /* Add expressions to a list, while keeping a count. */
1467 obstack_init (&vector_stack
);
1468 while ((c
= read_skip_spaces ()) && c
!= ']')
1471 fatal_expected_char (']', c
);
1474 obstack_ptr_grow (&vector_stack
, read_nested_rtx ());
1476 if (list_counter
> 0)
1478 return_vec
= rtvec_alloc (list_counter
);
1479 memcpy (&return_vec
->elem
[0], obstack_finish (&vector_stack
),
1480 list_counter
* sizeof (rtx
));
1482 else if (format_ptr
[idx
] == 'E')
1483 fatal_with_file_and_line ("vector must have at least one element");
1484 XVEC (return_rtx
, idx
) = return_vec
;
1485 obstack_free (&vector_stack
, NULL
);
1486 /* close bracket gotten */
1497 c
= read_skip_spaces ();
1501 /* 'S' fields are optional and should be NULL if no string
1502 was given. Also allow normal 's' and 'T' strings to be
1503 omitted, treating them in the same way as empty strings. */
1504 XSTR (return_rtx
, idx
) = (format_ptr
[idx
] == 'S' ? NULL
: "");
1508 /* The output template slot of a DEFINE_INSN,
1509 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
1510 gets a star inserted as its first character, if it is
1511 written with a brace block instead of a string constant. */
1512 star_if_braced
= (format_ptr
[idx
] == 'T');
1514 stringbuf
= read_string (star_if_braced
);
1518 #ifdef GENERATOR_FILE
1519 /* For insn patterns, we want to provide a default name
1520 based on the file and line, like "*foo.md:12", if the
1521 given name is blank. These are only for define_insn and
1522 define_insn_and_split, to aid debugging. */
1523 if (*stringbuf
== '\0'
1525 && (GET_CODE (return_rtx
) == DEFINE_INSN
1526 || GET_CODE (return_rtx
) == DEFINE_INSN_AND_SPLIT
))
1528 struct obstack
*string_obstack
= get_string_obstack ();
1530 const char *read_md_filename
= get_filename ();
1531 const char *fn
= (read_md_filename
? read_md_filename
: "rtx");
1533 for (slash
= fn
; *slash
; slash
++)
1534 if (*slash
== '/' || *slash
== '\\' || *slash
== ':')
1536 obstack_1grow (string_obstack
, '*');
1537 obstack_grow (string_obstack
, fn
, strlen (fn
));
1538 sprintf (line_name
, ":%d", get_lineno ());
1539 obstack_grow (string_obstack
, line_name
, strlen (line_name
)+1);
1540 stringbuf
= XOBFINISH (string_obstack
, char *);
1543 /* Find attr-names in the string. */
1545 char *start
, *end
, *ptr
;
1549 while ((start
= strchr (end
, '<')) && (end
= strchr (start
, '>')))
1551 if ((end
- start
- 1 > 0)
1552 && (end
- start
- 1 < (int)sizeof (tmpstr
)))
1554 strncpy (tmpstr
, start
+1, end
-start
-1);
1555 tmpstr
[end
-start
-1] = 0;
1561 = (struct mapping
*) htab_find (substs
.attrs
, &ptr
);
1564 /* Here we should find linked subst-iter. */
1565 str
= find_subst_iter_by_attr (ptr
);
1567 m
= (struct mapping
*) htab_find (substs
.iterators
, &str
);
1572 record_iterator_use (m
, return_rtx
, 0);
1574 #endif /* #ifdef GENERATOR_FILE */
1576 const char *string_ptr
= finalize_string (stringbuf
);
1579 XTMPL (return_rtx
, idx
) = string_ptr
;
1581 XSTR (return_rtx
, idx
) = string_ptr
;
1587 HOST_WIDE_INT tmp_wide
;
1589 validate_const_int (name
.string
);
1590 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1591 tmp_wide
= atoi (name
.string
);
1593 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1594 tmp_wide
= atol (name
.string
);
1596 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1597 But prefer not to use our hand-rolled function above either. */
1598 #if HAVE_DECL_ATOLL || !defined(HAVE_ATOQ)
1599 tmp_wide
= atoll (name
.string
);
1601 tmp_wide
= atoq (name
.string
);
1605 XWINT (return_rtx
, idx
) = tmp_wide
;
1611 /* Can be an iterator or an integer constant. */
1613 record_potential_iterator_use (&ints
, return_rtx
, idx
, name
.string
);
1618 validate_const_int (name
.string
);
1619 set_regno_raw (return_rtx
, atoi (name
.string
), 1);
1620 REG_ATTRS (return_rtx
) = NULL
;
1630 /* Read a nested rtx construct from the MD file and return it. */
1633 rtx_reader::read_nested_rtx ()
1635 struct md_name name
;
1638 /* In compact dumps, trailing "(nil)" values can be omitted.
1639 Handle such dumps. */
1640 if (peek_char () == ')')
1643 require_char_ws ('(');
1646 if (strcmp (name
.string
, "nil") == 0)
1649 return_rtx
= read_rtx_code (name
.string
);
1651 require_char_ws (')');
1653 return_rtx
= postprocess (return_rtx
);
1658 /* Mutually recursive subroutine of read_rtx which reads
1659 (thing x1 x2 x3 ...) and produces RTL as if
1660 (thing x1 (thing x2 (thing x3 ...))) had been written.
1661 When called, FORM is (thing x1 x2), and the file position
1662 is just past the leading parenthesis of x3. Only works
1663 for THINGs which are dyadic expressions, e.g. AND, IOR. */
1665 rtx_reader::read_rtx_variadic (rtx form
)
1674 q
= rtx_alloc (GET_CODE (p
));
1675 PUT_MODE (q
, GET_MODE (p
));
1677 XEXP (q
, 0) = XEXP (p
, 1);
1678 XEXP (q
, 1) = read_nested_rtx ();
1682 c
= read_skip_spaces ();
1689 /* Constructor for class rtx_reader. */
1691 rtx_reader::rtx_reader (bool compact
)
1692 : md_reader (compact
),
1693 m_in_call_function_usage (false)
1695 /* Set the global singleton pointer. */
1696 rtx_reader_ptr
= this;
1698 one_time_initialization ();
1701 /* Destructor for class rtx_reader. */
1703 rtx_reader::~rtx_reader ()
1705 /* Clear the global singleton pointer. */
1706 rtx_reader_ptr
= NULL
;