Daily bump.
[gcc.git] / gcc / read-rtl.c
1 /* RTL reader for GCC.
2 Copyright (C) 1987-2021 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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
9 version.
10
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
14 for more details.
15
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/>. */
19
20 /* This file is compiled twice: once for the generator programs
21 once for the compiler. */
22 #ifdef GENERATOR_FILE
23 #include "bconfig.h"
24 #else
25 #include "config.h"
26 #endif
27
28 /* Disable rtl checking; it conflicts with the iterator handling. */
29 #undef ENABLE_RTL_CHECKING
30
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "rtl.h"
35 #include "obstack.h"
36 #include "read-md.h"
37 #include "gensupport.h"
38
39 #ifndef GENERATOR_FILE
40 #include "function.h"
41 #include "memmodel.h"
42 #include "emit-rtl.h"
43 #endif
44
45 /* One element in a singly-linked list of (integer, string) pairs. */
46 struct map_value {
47 struct map_value *next;
48 int number;
49 const char *string;
50 };
51
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. */
55 struct mapping {
56 /* The name of the iterator or attribute. */
57 const char *name;
58
59 /* The group (modes or codes) to which the iterator or attribute belongs. */
60 struct iterator_group *group;
61
62 /* The list of (integer, string) pairs. */
63 struct map_value *values;
64
65 /* For iterators, records the current value of the iterator. */
66 struct map_value *current_value;
67 };
68
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
72 iterators. */
73 htab_t attrs, iterators;
74
75 /* The C++ type of the iterator, such as "machine_mode" for modes. */
76 const char *type;
77
78 /* Treat the given string as the name of a standard mode, etc., and
79 return its integer value. */
80 HOST_WIDE_INT (*find_builtin) (const char *);
81
82 /* Make the given rtx use the iterator value given by the third argument.
83 If the iterator applies to operands, the second argument gives the
84 operand index, otherwise it is ignored. */
85 void (*apply_iterator) (rtx, unsigned int, HOST_WIDE_INT);
86
87 /* Return the C token for the given standard mode, code, etc. */
88 const char *(*get_c_token) (int);
89 };
90
91 /* Records one use of an iterator. */
92 struct iterator_use {
93 /* The iterator itself. */
94 struct mapping *iterator;
95
96 /* The location of the use, as passed to the apply_iterator callback.
97 The index is the number of the operand that used the iterator
98 if applicable, otherwise it is ignored. */
99 rtx x;
100 unsigned int index;
101 };
102
103 /* Records one use of an attribute (the "<[iterator:]attribute>" syntax)
104 in a non-string rtx field. */
105 struct attribute_use {
106 /* The group that describes the use site. */
107 struct iterator_group *group;
108
109 /* The location at which the use occurs. */
110 file_location loc;
111
112 /* The name of the attribute, possibly with an "iterator:" prefix. */
113 const char *value;
114
115 /* The location of the use, as passed to GROUP's apply_iterator callback.
116 The index is the number of the operand that used the iterator
117 if applicable, otherwise it is ignored. */
118 rtx x;
119 unsigned int index;
120 };
121
122 /* This struct is used to link subst_attr named ATTR_NAME with
123 corresponding define_subst named ITER_NAME. */
124 struct subst_attr_to_iter_mapping
125 {
126 char *attr_name;
127 char *iter_name;
128 };
129
130 /* Hash-table to store links between subst-attributes and
131 define_substs. */
132 htab_t subst_attr_to_iter_map = NULL;
133 /* This global stores name of subst-iterator which is currently being
134 processed. */
135 const char *current_iterator_name;
136
137 static void validate_const_int (const char *);
138 static void one_time_initialization (void);
139
140 /* Global singleton. */
141 rtx_reader *rtx_reader_ptr = NULL;
142 \f
143 /* The mode and code iterator structures. */
144 static struct iterator_group modes, codes, ints, substs;
145
146 /* All iterators used in the current rtx. */
147 static vec<mapping *> current_iterators;
148
149 /* The list of all iterator uses in the current rtx. */
150 static vec<iterator_use> iterator_uses;
151
152 /* The list of all attribute uses in the current rtx. */
153 static vec<attribute_use> attribute_uses;
154
155 /* Provide a version of a function to read a long long if the system does
156 not provide one. */
157 #if (HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG \
158 && !HAVE_DECL_ATOLL \
159 && !defined (HAVE_ATOQ))
160 HOST_WIDE_INT atoll (const char *);
161
162 HOST_WIDE_INT
163 atoll (const char *p)
164 {
165 int neg = 0;
166 HOST_WIDE_INT tmp_wide;
167
168 while (ISSPACE (*p))
169 p++;
170 if (*p == '-')
171 neg = 1, p++;
172 else if (*p == '+')
173 p++;
174
175 tmp_wide = 0;
176 while (ISDIGIT (*p))
177 {
178 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
179 if (new_wide < tmp_wide)
180 {
181 /* Return INT_MAX equiv on overflow. */
182 tmp_wide = HOST_WIDE_INT_M1U >> 1;
183 break;
184 }
185 tmp_wide = new_wide;
186 p++;
187 }
188
189 if (neg)
190 tmp_wide = -tmp_wide;
191 return tmp_wide;
192 }
193 #endif
194
195 /* Implementations of the iterator_group callbacks for modes. */
196
197 static HOST_WIDE_INT
198 find_mode (const char *name)
199 {
200 int i;
201
202 for (i = 0; i < NUM_MACHINE_MODES; i++)
203 if (strcmp (GET_MODE_NAME (i), name) == 0)
204 return i;
205
206 fatal_with_file_and_line ("unknown mode `%s'", name);
207 }
208
209 static void
210 apply_mode_iterator (rtx x, unsigned int, HOST_WIDE_INT mode)
211 {
212 PUT_MODE (x, (machine_mode) mode);
213 }
214
215 static const char *
216 get_mode_token (int mode)
217 {
218 return concat ("E_", GET_MODE_NAME (mode), "mode", NULL);
219 }
220
221 /* In compact dumps, the code of insns is prefixed with "c", giving "cinsn",
222 "cnote" etc, and CODE_LABEL is special-cased as "clabel". */
223
224 struct compact_insn_name {
225 RTX_CODE code;
226 const char *name;
227 };
228
229 static const compact_insn_name compact_insn_names[] = {
230 { DEBUG_INSN, "cdebug_insn" },
231 { INSN, "cinsn" },
232 { JUMP_INSN, "cjump_insn" },
233 { CALL_INSN, "ccall_insn" },
234 { JUMP_TABLE_DATA, "cjump_table_data" },
235 { BARRIER, "cbarrier" },
236 { CODE_LABEL, "clabel" },
237 { NOTE, "cnote" }
238 };
239
240 /* Return the rtx code for NAME, or UNKNOWN if NAME isn't a valid rtx code. */
241
242 static rtx_code
243 maybe_find_code (const char *name)
244 {
245 for (int i = 0; i < NUM_RTX_CODE; i++)
246 if (strcmp (GET_RTX_NAME (i), name) == 0)
247 return (rtx_code) i;
248
249 for (int i = 0; i < (signed)ARRAY_SIZE (compact_insn_names); i++)
250 if (strcmp (compact_insn_names[i].name, name) == 0)
251 return compact_insn_names[i].code;
252
253 return UNKNOWN;
254 }
255
256 /* Implementations of the iterator_group callbacks for codes. */
257
258 static HOST_WIDE_INT
259 find_code (const char *name)
260 {
261 rtx_code code = maybe_find_code (name);
262 if (code == UNKNOWN)
263 fatal_with_file_and_line ("unknown rtx code `%s'", name);
264 return code;
265 }
266
267 static void
268 apply_code_iterator (rtx x, unsigned int, HOST_WIDE_INT code)
269 {
270 PUT_CODE (x, (enum rtx_code) code);
271 }
272
273 static const char *
274 get_code_token (int code)
275 {
276 char *name = xstrdup (GET_RTX_NAME (code));
277 for (int i = 0; name[i]; ++i)
278 name[i] = TOUPPER (name[i]);
279 return name;
280 }
281
282 /* Implementations of the iterator_group callbacks for ints. */
283
284 /* Since GCC does not construct a table of valid constants,
285 we have to accept any int as valid. No cross-checking can
286 be done. */
287
288 static HOST_WIDE_INT
289 find_int (const char *name)
290 {
291 HOST_WIDE_INT tmp;
292
293 validate_const_int (name);
294 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
295 tmp = atoi (name);
296 #else
297 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
298 tmp = atol (name);
299 #else
300 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
301 But prefer not to use our hand-rolled function above either. */
302 #if HAVE_DECL_ATOLL || !defined(HAVE_ATOQ)
303 tmp = atoll (name);
304 #else
305 tmp = atoq (name);
306 #endif
307 #endif
308 #endif
309 return tmp;
310 }
311
312 static void
313 apply_int_iterator (rtx x, unsigned int index, HOST_WIDE_INT value)
314 {
315 RTX_CODE code = GET_CODE (x);
316 const char *format_ptr = GET_RTX_FORMAT (code);
317
318 switch (format_ptr[index])
319 {
320 case 'i':
321 case 'n':
322 XINT (x, index) = value;
323 break;
324 case 'w':
325 XWINT (x, index) = value;
326 break;
327 case 'p':
328 gcc_assert (code == SUBREG);
329 SUBREG_BYTE (x) = value;
330 break;
331 default:
332 gcc_unreachable ();
333 }
334 }
335
336 static const char *
337 get_int_token (int value)
338 {
339 char buffer[HOST_BITS_PER_INT + 1];
340 sprintf (buffer, "%d", value);
341 return xstrdup (buffer);
342 }
343
344 #ifdef GENERATOR_FILE
345
346 /* This routine adds attribute or does nothing depending on VALUE. When
347 VALUE is 1, it does nothing - the first duplicate of original
348 template is kept untouched when it's subjected to a define_subst.
349 When VALUE isn't 1, the routine modifies RTL-template RT, adding
350 attribute, named exactly as define_subst, which later will be
351 applied. If such attribute has already been added, then no the
352 routine has no effect. */
353 static void
354 apply_subst_iterator (rtx rt, unsigned int, HOST_WIDE_INT value)
355 {
356 rtx new_attr;
357 rtvec attrs_vec, new_attrs_vec;
358 int i;
359 /* define_split has no attributes. */
360 if (value == 1 || GET_CODE (rt) == DEFINE_SPLIT)
361 return;
362 gcc_assert (GET_CODE (rt) == DEFINE_INSN
363 || GET_CODE (rt) == DEFINE_INSN_AND_SPLIT
364 || GET_CODE (rt) == DEFINE_INSN_AND_REWRITE
365 || GET_CODE (rt) == DEFINE_EXPAND);
366
367 int attrs = (GET_CODE (rt) == DEFINE_INSN_AND_SPLIT ? 7
368 : GET_CODE (rt) == DEFINE_INSN_AND_REWRITE ? 6 : 4);
369 attrs_vec = XVEC (rt, attrs);
370
371 /* If we've already added attribute 'current_iterator_name', then we
372 have nothing to do now. */
373 if (attrs_vec)
374 {
375 for (i = 0; i < GET_NUM_ELEM (attrs_vec); i++)
376 {
377 if (strcmp (XSTR (attrs_vec->elem[i], 0), current_iterator_name) == 0)
378 return;
379 }
380 }
381
382 /* Add attribute with subst name - it serves as a mark for
383 define_subst which later would be applied to this pattern. */
384 new_attr = rtx_alloc (SET_ATTR);
385 PUT_CODE (new_attr, SET_ATTR);
386 XSTR (new_attr, 0) = xstrdup (current_iterator_name);
387 XSTR (new_attr, 1) = xstrdup ("yes");
388
389 if (!attrs_vec)
390 {
391 new_attrs_vec = rtvec_alloc (1);
392 new_attrs_vec->elem[0] = new_attr;
393 }
394 else
395 {
396 new_attrs_vec = rtvec_alloc (GET_NUM_ELEM (attrs_vec) + 1);
397 memcpy (&new_attrs_vec->elem[0], &attrs_vec->elem[0],
398 GET_NUM_ELEM (attrs_vec) * sizeof (rtx));
399 new_attrs_vec->elem[GET_NUM_ELEM (attrs_vec)] = new_attr;
400 }
401 XVEC (rt, attrs) = new_attrs_vec;
402 }
403
404 /* Map subst-attribute ATTR to subst iterator ITER. */
405
406 static void
407 bind_subst_iter_and_attr (const char *iter, const char *attr)
408 {
409 struct subst_attr_to_iter_mapping *value;
410 void **slot;
411 if (!subst_attr_to_iter_map)
412 subst_attr_to_iter_map =
413 htab_create (1, leading_string_hash, leading_string_eq_p, 0);
414 value = XNEW (struct subst_attr_to_iter_mapping);
415 value->attr_name = xstrdup (attr);
416 value->iter_name = xstrdup (iter);
417 slot = htab_find_slot (subst_attr_to_iter_map, value, INSERT);
418 *slot = value;
419 }
420
421 #endif /* #ifdef GENERATOR_FILE */
422
423 /* Return name of a subst-iterator, corresponding to subst-attribute ATTR. */
424
425 static char*
426 find_subst_iter_by_attr (const char *attr)
427 {
428 char *iter_name = NULL;
429 struct subst_attr_to_iter_mapping *value;
430 value = (struct subst_attr_to_iter_mapping*)
431 htab_find (subst_attr_to_iter_map, &attr);
432 if (value)
433 iter_name = value->iter_name;
434 return iter_name;
435 }
436
437 /* Map attribute string P to its current value. Return null if the attribute
438 isn't known. If ITERATOR_OUT is nonnull, store the associated iterator
439 there. Report any errors against location LOC. */
440
441 static struct map_value *
442 map_attr_string (file_location loc, const char *p, mapping **iterator_out = 0)
443 {
444 const char *attr;
445 struct mapping *iterator;
446 unsigned int i;
447 struct mapping *m;
448 struct map_value *v;
449 int iterator_name_len;
450 struct map_value *res = NULL;
451 struct mapping *prev = NULL;
452
453 /* Peel off any "iterator:" prefix. Set ATTR to the start of the
454 attribute name. */
455 attr = strchr (p, ':');
456 if (attr == 0)
457 {
458 iterator_name_len = -1;
459 attr = p;
460 }
461 else
462 {
463 iterator_name_len = attr - p;
464 attr++;
465 }
466
467 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
468 {
469 /* If an iterator name was specified, check that it matches. */
470 if (iterator_name_len >= 0
471 && (strncmp (p, iterator->name, iterator_name_len) != 0
472 || iterator->name[iterator_name_len] != 0))
473 continue;
474
475 /* Find the attribute specification. */
476 m = (struct mapping *) htab_find (iterator->group->attrs, &attr);
477 if (m)
478 {
479 /* In contrast to code/mode/int iterators, attributes of subst
480 iterators are linked to one specific subst-iterator. So, if
481 we are dealing with subst-iterator, we should check if it's
482 the one which linked with the given attribute. */
483 if (iterator->group == &substs)
484 {
485 char *iter_name = find_subst_iter_by_attr (attr);
486 if (strcmp (iter_name, iterator->name) != 0)
487 continue;
488 }
489 /* Find the attribute value associated with the current
490 iterator value. */
491 for (v = m->values; v; v = v->next)
492 if (v->number == iterator->current_value->number)
493 {
494 if (res && strcmp (v->string, res->string) != 0)
495 {
496 error_at (loc, "ambiguous attribute '%s'; could be"
497 " '%s' (via '%s:%s') or '%s' (via '%s:%s')",
498 attr, res->string, prev->name, attr,
499 v->string, iterator->name, attr);
500 return v;
501 }
502 if (iterator_out)
503 *iterator_out = iterator;
504 prev = iterator;
505 res = v;
506 }
507 }
508 }
509 return res;
510 }
511
512 /* Apply the current iterator values to STRING. Return the new string
513 if any changes were needed, otherwise return STRING itself. */
514
515 const char *
516 md_reader::apply_iterator_to_string (const char *string)
517 {
518 char *base, *copy, *p, *start, *end;
519 struct map_value *v;
520
521 if (string == 0 || string[0] == 0)
522 return string;
523
524 file_location loc = get_md_ptr_loc (string)->loc;
525 base = p = copy = ASTRDUP (string);
526 while ((start = strchr (p, '<')) && (end = strchr (start, '>')))
527 {
528 p = start + 1;
529
530 *end = 0;
531 v = map_attr_string (loc, p);
532 *end = '>';
533 if (v == 0)
534 continue;
535
536 /* Add everything between the last copied byte and the '<',
537 then add in the attribute value. */
538 obstack_grow (&m_string_obstack, base, start - base);
539 obstack_grow (&m_string_obstack, v->string, strlen (v->string));
540 base = end + 1;
541 }
542 if (base != copy)
543 {
544 obstack_grow (&m_string_obstack, base, strlen (base) + 1);
545 copy = XOBFINISH (&m_string_obstack, char *);
546 copy_md_ptr_loc (copy, string);
547 return copy;
548 }
549 return string;
550 }
551
552 /* Return a deep copy of X, substituting the current iterator
553 values into any strings. */
554
555 rtx
556 md_reader::copy_rtx_for_iterators (rtx original)
557 {
558 const char *format_ptr, *p;
559 int i, j;
560 rtx x;
561
562 if (original == 0)
563 return original;
564
565 /* Create a shallow copy of ORIGINAL. */
566 x = rtx_alloc (GET_CODE (original));
567 memcpy (x, original, RTX_CODE_SIZE (GET_CODE (original)));
568
569 /* Change each string and recursively change each rtx. */
570 format_ptr = GET_RTX_FORMAT (GET_CODE (original));
571 for (i = 0; format_ptr[i] != 0; i++)
572 switch (format_ptr[i])
573 {
574 case 'T':
575 while (XTMPL (x, i) != (p = apply_iterator_to_string (XTMPL (x, i))))
576 XTMPL (x, i) = p;
577 break;
578
579 case 'S':
580 case 's':
581 while (XSTR (x, i) != (p = apply_iterator_to_string (XSTR (x, i))))
582 XSTR (x, i) = p;
583 break;
584
585 case 'e':
586 XEXP (x, i) = copy_rtx_for_iterators (XEXP (x, i));
587 break;
588
589 case 'V':
590 case 'E':
591 if (XVEC (original, i))
592 {
593 XVEC (x, i) = rtvec_alloc (XVECLEN (original, i));
594 for (j = 0; j < XVECLEN (x, i); j++)
595 XVECEXP (x, i, j)
596 = copy_rtx_for_iterators (XVECEXP (original, i, j));
597 }
598 break;
599
600 default:
601 break;
602 }
603 return x;
604 }
605
606 #ifdef GENERATOR_FILE
607
608 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
609 has the form "&& ..." (as used in define_insn_and_splits), assume that
610 EXTRA is already satisfied. Empty strings are treated like "true". */
611
612 static const char *
613 add_condition_to_string (const char *original, const char *extra)
614 {
615 if (original != 0 && original[0] == '&' && original[1] == '&')
616 return original;
617 return rtx_reader_ptr->join_c_conditions (original, extra);
618 }
619
620 /* Like add_condition, but applied to all conditions in rtx X. */
621
622 static void
623 add_condition_to_rtx (rtx x, const char *extra)
624 {
625 switch (GET_CODE (x))
626 {
627 case DEFINE_INSN:
628 case DEFINE_EXPAND:
629 case DEFINE_SUBST:
630 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
631 break;
632
633 case DEFINE_SPLIT:
634 case DEFINE_PEEPHOLE:
635 case DEFINE_PEEPHOLE2:
636 case DEFINE_COND_EXEC:
637 XSTR (x, 1) = add_condition_to_string (XSTR (x, 1), extra);
638 break;
639
640 case DEFINE_INSN_AND_SPLIT:
641 case DEFINE_INSN_AND_REWRITE:
642 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
643 XSTR (x, 4) = add_condition_to_string (XSTR (x, 4), extra);
644 break;
645
646 default:
647 break;
648 }
649 }
650
651 /* Apply the current iterator values to all attribute_uses. */
652
653 static void
654 apply_attribute_uses (void)
655 {
656 struct map_value *v;
657 attribute_use *ause;
658 unsigned int i;
659
660 FOR_EACH_VEC_ELT (attribute_uses, i, ause)
661 {
662 v = map_attr_string (ause->loc, ause->value);
663 if (!v)
664 fatal_with_file_and_line ("unknown iterator value `%s'", ause->value);
665 ause->group->apply_iterator (ause->x, ause->index,
666 ause->group->find_builtin (v->string));
667 }
668 }
669
670 /* A htab_traverse callback for iterators. Add all used iterators
671 to current_iterators. */
672
673 static int
674 add_current_iterators (void **slot, void *data ATTRIBUTE_UNUSED)
675 {
676 struct mapping *iterator;
677
678 iterator = (struct mapping *) *slot;
679 if (iterator->current_value)
680 current_iterators.safe_push (iterator);
681 return 1;
682 }
683
684 /* Return a hash value for overloaded_name UNCAST_ONAME. There shouldn't
685 be many instances of two overloaded_names having the same name but
686 different arguments, so hashing on the name should be good enough in
687 practice. */
688
689 static hashval_t
690 overloaded_name_hash (const void *uncast_oname)
691 {
692 const overloaded_name *oname = (const overloaded_name *) uncast_oname;
693 return htab_hash_string (oname->name);
694 }
695
696 /* Return true if two overloaded_names are similar enough to share
697 the same generated functions. */
698
699 static int
700 overloaded_name_eq_p (const void *uncast_oname1, const void *uncast_oname2)
701 {
702 const overloaded_name *oname1 = (const overloaded_name *) uncast_oname1;
703 const overloaded_name *oname2 = (const overloaded_name *) uncast_oname2;
704 if (strcmp (oname1->name, oname2->name) != 0
705 || oname1->arg_types.length () != oname2->arg_types.length ())
706 return 0;
707
708 for (unsigned int i = 0; i < oname1->arg_types.length (); ++i)
709 if (strcmp (oname1->arg_types[i], oname2->arg_types[i]) != 0)
710 return 0;
711
712 return 1;
713 }
714
715 /* Return true if X has an instruction name in XSTR (X, 0). */
716
717 static bool
718 named_rtx_p (rtx x)
719 {
720 switch (GET_CODE (x))
721 {
722 case DEFINE_EXPAND:
723 case DEFINE_INSN:
724 case DEFINE_INSN_AND_SPLIT:
725 case DEFINE_INSN_AND_REWRITE:
726 return true;
727
728 default:
729 return false;
730 }
731 }
732
733 /* Check whether ORIGINAL is a named pattern whose name starts with '@'.
734 If so, return the associated overloaded_name and add the iterator for
735 each argument to ITERATORS. Return null otherwise. */
736
737 overloaded_name *
738 md_reader::handle_overloaded_name (rtx original, vec<mapping *> *iterators)
739 {
740 /* Check for the leading '@'. */
741 if (!named_rtx_p (original) || XSTR (original, 0)[0] != '@')
742 return NULL;
743
744 /* Remove the '@', so that no other code needs to worry about it. */
745 const char *name = XSTR (original, 0);
746 file_location loc = get_md_ptr_loc (name)->loc;
747 copy_md_ptr_loc (name + 1, name);
748 name += 1;
749 XSTR (original, 0) = name;
750
751 /* Build a copy of the name without the '<...>' attribute strings.
752 Add the iterator associated with each such attribute string to ITERATORS
753 and add an associated argument to TMP_ONAME. */
754 char *copy = ASTRDUP (name);
755 char *base = copy, *start, *end;
756 overloaded_name tmp_oname;
757 tmp_oname.arg_types.create (current_iterators.length ());
758 bool pending_underscore_p = false;
759 while ((start = strchr (base, '<')) && (end = strchr (start, '>')))
760 {
761 *end = 0;
762 mapping *iterator;
763 if (!map_attr_string (loc, start + 1, &iterator))
764 fatal_with_file_and_line ("unknown iterator `%s'", start + 1);
765 *end = '>';
766
767 /* Remove a trailing underscore, so that we don't end a name
768 with "_" or turn "_<...>_" into "__". */
769 if (start != base && start[-1] == '_')
770 {
771 start -= 1;
772 pending_underscore_p = true;
773 }
774
775 /* Add the text between either the last '>' or the start of
776 the string and this '<'. */
777 obstack_grow (&m_string_obstack, base, start - base);
778 base = end + 1;
779
780 /* If there's a character we need to keep after the '>', check
781 whether we should prefix it with a previously-dropped '_'. */
782 if (base[0] != 0 && base[0] != '<')
783 {
784 if (pending_underscore_p && base[0] != '_')
785 obstack_1grow (&m_string_obstack, '_');
786 pending_underscore_p = false;
787 }
788
789 /* Record an argument for ITERATOR. */
790 iterators->safe_push (iterator);
791 tmp_oname.arg_types.safe_push (iterator->group->type);
792 }
793 if (base == copy)
794 fatal_with_file_and_line ("no iterator attributes in name `%s'", name);
795
796 size_t length = obstack_object_size (&m_string_obstack);
797 if (length == 0)
798 fatal_with_file_and_line ("`%s' only contains iterator attributes", name);
799
800 /* Get the completed name. */
801 obstack_grow (&m_string_obstack, base, strlen (base) + 1);
802 char *new_name = XOBFINISH (&m_string_obstack, char *);
803 tmp_oname.name = new_name;
804
805 if (!m_overloads_htab)
806 m_overloads_htab = htab_create (31, overloaded_name_hash,
807 overloaded_name_eq_p, NULL);
808
809 /* See whether another pattern had the same overload name and list
810 of argument types. Create a new permanent one if not. */
811 void **slot = htab_find_slot (m_overloads_htab, &tmp_oname, INSERT);
812 overloaded_name *oname = (overloaded_name *) *slot;
813 if (!oname)
814 {
815 *slot = oname = new overloaded_name;
816 oname->name = tmp_oname.name;
817 oname->arg_types = tmp_oname.arg_types;
818 oname->next = NULL;
819 oname->first_instance = NULL;
820 oname->next_instance_ptr = &oname->first_instance;
821
822 *m_next_overload_ptr = oname;
823 m_next_overload_ptr = &oname->next;
824 }
825 else
826 {
827 obstack_free (&m_string_obstack, new_name);
828 tmp_oname.arg_types.release ();
829 }
830
831 return oname;
832 }
833
834 /* Add an instance of ONAME for instruction pattern X. ITERATORS[I]
835 gives the iterator associated with argument I of ONAME. */
836
837 static void
838 add_overload_instance (overloaded_name *oname, vec<mapping *> iterators, rtx x)
839 {
840 /* Create the instance. */
841 overloaded_instance *instance = new overloaded_instance;
842 instance->next = NULL;
843 instance->arg_values.create (oname->arg_types.length ());
844 for (unsigned int i = 0; i < iterators.length (); ++i)
845 {
846 int value = iterators[i]->current_value->number;
847 const char *name = iterators[i]->group->get_c_token (value);
848 instance->arg_values.quick_push (name);
849 }
850 instance->name = XSTR (x, 0);
851 instance->insn = x;
852
853 /* Chain it onto the end of ONAME's list. */
854 *oname->next_instance_ptr = instance;
855 oname->next_instance_ptr = &instance->next;
856 }
857
858 /* Expand all iterators in the current rtx, which is given as ORIGINAL.
859 Build a list of expanded rtxes in the EXPR_LIST pointed to by QUEUE. */
860
861 static void
862 apply_iterators (rtx original, vec<rtx> *queue)
863 {
864 unsigned int i;
865 const char *condition;
866 iterator_use *iuse;
867 struct mapping *iterator;
868 struct map_value *v;
869 rtx x;
870
871 if (iterator_uses.is_empty ())
872 {
873 /* Raise an error if any attributes were used. */
874 apply_attribute_uses ();
875
876 if (named_rtx_p (original) && XSTR (original, 0)[0] == '@')
877 fatal_with_file_and_line ("'@' used without iterators");
878
879 queue->safe_push (original);
880 return;
881 }
882
883 /* Clear out the iterators from the previous run. */
884 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
885 iterator->current_value = NULL;
886 current_iterators.truncate (0);
887
888 /* Mark the iterators that we need this time. */
889 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
890 iuse->iterator->current_value = iuse->iterator->values;
891
892 /* Get the list of iterators that are in use, preserving the
893 definition order within each group. */
894 htab_traverse (modes.iterators, add_current_iterators, NULL);
895 htab_traverse (codes.iterators, add_current_iterators, NULL);
896 htab_traverse (ints.iterators, add_current_iterators, NULL);
897 htab_traverse (substs.iterators, add_current_iterators, NULL);
898 gcc_assert (!current_iterators.is_empty ());
899
900 /* Check whether this is a '@' overloaded pattern. */
901 auto_vec<mapping *, 16> iterators;
902 overloaded_name *oname
903 = rtx_reader_ptr->handle_overloaded_name (original, &iterators);
904
905 for (;;)
906 {
907 /* Apply the current iterator values. Accumulate a condition to
908 say when the resulting rtx can be used. */
909 condition = "";
910 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
911 {
912 if (iuse->iterator->group == &substs)
913 continue;
914 v = iuse->iterator->current_value;
915 iuse->iterator->group->apply_iterator (iuse->x, iuse->index,
916 v->number);
917 condition = rtx_reader_ptr->join_c_conditions (condition, v->string);
918 }
919 apply_attribute_uses ();
920 x = rtx_reader_ptr->copy_rtx_for_iterators (original);
921 add_condition_to_rtx (x, condition);
922
923 /* We apply subst iterator after RTL-template is copied, as during
924 subst-iterator processing, we could add an attribute to the
925 RTL-template, and we don't want to do it in the original one. */
926 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
927 {
928 v = iuse->iterator->current_value;
929 if (iuse->iterator->group == &substs)
930 {
931 iuse->x = x;
932 iuse->index = 0;
933 current_iterator_name = iuse->iterator->name;
934 iuse->iterator->group->apply_iterator (iuse->x, iuse->index,
935 v->number);
936 }
937 }
938
939 if (oname)
940 add_overload_instance (oname, iterators, x);
941
942 /* Add the new rtx to the end of the queue. */
943 queue->safe_push (x);
944
945 /* Lexicographically increment the iterator value sequence.
946 That is, cycle through iterator values, starting from the right,
947 and stopping when one of them doesn't wrap around. */
948 i = current_iterators.length ();
949 for (;;)
950 {
951 if (i == 0)
952 return;
953 i--;
954 iterator = current_iterators[i];
955 iterator->current_value = iterator->current_value->next;
956 if (iterator->current_value)
957 break;
958 iterator->current_value = iterator->values;
959 }
960 }
961 }
962 #endif /* #ifdef GENERATOR_FILE */
963
964 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
965 of the mapping and GROUP is the group to which it belongs. */
966
967 static struct mapping *
968 add_mapping (struct iterator_group *group, htab_t table, const char *name)
969 {
970 struct mapping *m;
971 void **slot;
972
973 m = XNEW (struct mapping);
974 m->name = xstrdup (name);
975 m->group = group;
976 m->values = 0;
977 m->current_value = NULL;
978
979 slot = htab_find_slot (table, m, INSERT);
980 if (*slot != 0)
981 fatal_with_file_and_line ("`%s' already defined", name);
982
983 *slot = m;
984 return m;
985 }
986
987 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
988 END_PTR points to the current null terminator for the list; return
989 a pointer the new null terminator. */
990
991 static struct map_value **
992 add_map_value (struct map_value **end_ptr, int number, const char *string)
993 {
994 struct map_value *value;
995
996 value = XNEW (struct map_value);
997 value->next = 0;
998 value->number = number;
999 value->string = string;
1000
1001 *end_ptr = value;
1002 return &value->next;
1003 }
1004
1005 /* Do one-time initialization of the mode and code attributes. */
1006
1007 static void
1008 initialize_iterators (void)
1009 {
1010 struct mapping *lower, *upper;
1011 struct map_value **lower_ptr, **upper_ptr;
1012 char *copy, *p;
1013 int i;
1014
1015 modes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
1016 modes.iterators = htab_create (13, leading_string_hash,
1017 leading_string_eq_p, 0);
1018 modes.type = "machine_mode";
1019 modes.find_builtin = find_mode;
1020 modes.apply_iterator = apply_mode_iterator;
1021 modes.get_c_token = get_mode_token;
1022
1023 codes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
1024 codes.iterators = htab_create (13, leading_string_hash,
1025 leading_string_eq_p, 0);
1026 codes.type = "rtx_code";
1027 codes.find_builtin = find_code;
1028 codes.apply_iterator = apply_code_iterator;
1029 codes.get_c_token = get_code_token;
1030
1031 ints.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
1032 ints.iterators = htab_create (13, leading_string_hash,
1033 leading_string_eq_p, 0);
1034 ints.type = "int";
1035 ints.find_builtin = find_int;
1036 ints.apply_iterator = apply_int_iterator;
1037 ints.get_c_token = get_int_token;
1038
1039 substs.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
1040 substs.iterators = htab_create (13, leading_string_hash,
1041 leading_string_eq_p, 0);
1042 substs.type = "int";
1043 substs.find_builtin = find_int; /* We don't use it, anyway. */
1044 #ifdef GENERATOR_FILE
1045 substs.apply_iterator = apply_subst_iterator;
1046 #endif
1047 substs.get_c_token = get_int_token;
1048
1049 lower = add_mapping (&modes, modes.attrs, "mode");
1050 upper = add_mapping (&modes, modes.attrs, "MODE");
1051 lower_ptr = &lower->values;
1052 upper_ptr = &upper->values;
1053 for (i = 0; i < MAX_MACHINE_MODE; i++)
1054 {
1055 copy = xstrdup (GET_MODE_NAME (i));
1056 for (p = copy; *p != 0; p++)
1057 *p = TOLOWER (*p);
1058
1059 upper_ptr = add_map_value (upper_ptr, i, GET_MODE_NAME (i));
1060 lower_ptr = add_map_value (lower_ptr, i, copy);
1061 }
1062
1063 lower = add_mapping (&codes, codes.attrs, "code");
1064 upper = add_mapping (&codes, codes.attrs, "CODE");
1065 lower_ptr = &lower->values;
1066 upper_ptr = &upper->values;
1067 for (i = 0; i < NUM_RTX_CODE; i++)
1068 {
1069 copy = xstrdup (GET_RTX_NAME (i));
1070 for (p = copy; *p != 0; p++)
1071 *p = TOUPPER (*p);
1072
1073 lower_ptr = add_map_value (lower_ptr, i, GET_RTX_NAME (i));
1074 upper_ptr = add_map_value (upper_ptr, i, copy);
1075 }
1076 }
1077 \f
1078
1079 #ifdef GENERATOR_FILE
1080 /* Process a define_conditions directive, starting with the optional
1081 space after the "define_conditions". The directive looks like this:
1082
1083 (define_conditions [
1084 (number "string")
1085 (number "string")
1086 ...
1087 ])
1088
1089 It's not intended to appear in machine descriptions. It is
1090 generated by (the program generated by) genconditions.c, and
1091 slipped in at the beginning of the sequence of MD files read by
1092 most of the other generators. */
1093 void
1094 md_reader::read_conditions ()
1095 {
1096 int c;
1097
1098 require_char_ws ('[');
1099
1100 while ( (c = read_skip_spaces ()) != ']')
1101 {
1102 struct md_name name;
1103 char *expr;
1104 int value;
1105
1106 if (c != '(')
1107 fatal_expected_char ('(', c);
1108
1109 read_name (&name);
1110 validate_const_int (name.string);
1111 value = atoi (name.string);
1112
1113 require_char_ws ('"');
1114 expr = read_quoted_string ();
1115
1116 require_char_ws (')');
1117
1118 add_c_test (expr, value);
1119 }
1120 }
1121 #endif /* #ifdef GENERATOR_FILE */
1122
1123 static void
1124 validate_const_int (const char *string)
1125 {
1126 const char *cp;
1127 int valid = 1;
1128
1129 cp = string;
1130 while (*cp && ISSPACE (*cp))
1131 cp++;
1132 if (*cp == '-' || *cp == '+')
1133 cp++;
1134 if (*cp == 0)
1135 valid = 0;
1136 for (; *cp; cp++)
1137 if (! ISDIGIT (*cp))
1138 {
1139 valid = 0;
1140 break;
1141 }
1142 if (!valid)
1143 fatal_with_file_and_line ("invalid decimal constant \"%s\"\n", string);
1144 }
1145
1146 static void
1147 validate_const_wide_int (const char *string)
1148 {
1149 const char *cp;
1150 int valid = 1;
1151
1152 cp = string;
1153 while (*cp && ISSPACE (*cp))
1154 cp++;
1155 /* Skip the leading 0x. */
1156 if (cp[0] == '0' || cp[1] == 'x')
1157 cp += 2;
1158 else
1159 valid = 0;
1160 if (*cp == 0)
1161 valid = 0;
1162 for (; *cp; cp++)
1163 if (! ISXDIGIT (*cp))
1164 valid = 0;
1165 if (!valid)
1166 fatal_with_file_and_line ("invalid hex constant \"%s\"\n", string);
1167 }
1168
1169 /* Record that X uses iterator ITERATOR. If the use is in an operand
1170 of X, INDEX is the index of that operand, otherwise it is ignored. */
1171
1172 static void
1173 record_iterator_use (struct mapping *iterator, rtx x, unsigned int index)
1174 {
1175 struct iterator_use iuse = {iterator, x, index};
1176 iterator_uses.safe_push (iuse);
1177 }
1178
1179 /* Record that X uses attribute VALUE at location LOC, where VALUE must
1180 match a built-in value from group GROUP. If the use is in an operand
1181 of X, INDEX is the index of that operand, otherwise it is ignored. */
1182
1183 static void
1184 record_attribute_use (struct iterator_group *group, file_location loc, rtx x,
1185 unsigned int index, const char *value)
1186 {
1187 struct attribute_use ause = {group, loc, value, x, index};
1188 attribute_uses.safe_push (ause);
1189 }
1190
1191 /* Interpret NAME as either a built-in value, iterator or attribute
1192 for group GROUP. X and INDEX are the values to pass to GROUP's
1193 apply_iterator callback. LOC is the location of the use. */
1194
1195 void
1196 md_reader::record_potential_iterator_use (struct iterator_group *group,
1197 file_location loc,
1198 rtx x, unsigned int index,
1199 const char *name)
1200 {
1201 struct mapping *m;
1202 size_t len;
1203
1204 len = strlen (name);
1205 if (name[0] == '<' && name[len - 1] == '>')
1206 {
1207 /* Copy the attribute string into permanent storage, without the
1208 angle brackets around it. */
1209 obstack_grow0 (&m_string_obstack, name + 1, len - 2);
1210 record_attribute_use (group, loc, x, index,
1211 XOBFINISH (&m_string_obstack, char *));
1212 }
1213 else
1214 {
1215 m = (struct mapping *) htab_find (group->iterators, &name);
1216 if (m != 0)
1217 record_iterator_use (m, x, index);
1218 else
1219 group->apply_iterator (x, index, group->find_builtin (name));
1220 }
1221 }
1222
1223 #ifdef GENERATOR_FILE
1224
1225 /* Finish reading a declaration of the form:
1226
1227 (define... <name> [<value1> ... <valuen>])
1228
1229 from the MD file, where each <valuei> is either a bare symbol name or a
1230 "(<name> <string>)" pair. The "(define..." part has already been read.
1231
1232 Represent the declaration as a "mapping" structure; add it to TABLE
1233 (which belongs to GROUP) and return it. */
1234
1235 struct mapping *
1236 md_reader::read_mapping (struct iterator_group *group, htab_t table)
1237 {
1238 struct md_name name;
1239 struct mapping *m;
1240 struct map_value **end_ptr;
1241 const char *string;
1242 int number, c;
1243
1244 /* Read the mapping name and create a structure for it. */
1245 read_name (&name);
1246 m = add_mapping (group, table, name.string);
1247
1248 require_char_ws ('[');
1249
1250 /* Read each value. */
1251 end_ptr = &m->values;
1252 c = read_skip_spaces ();
1253 do
1254 {
1255 if (c != '(')
1256 {
1257 /* A bare symbol name that is implicitly paired to an
1258 empty string. */
1259 unread_char (c);
1260 read_name (&name);
1261 string = "";
1262 }
1263 else
1264 {
1265 /* A "(name string)" pair. */
1266 read_name (&name);
1267 string = read_string (false);
1268 require_char_ws (')');
1269 }
1270 number = group->find_builtin (name.string);
1271 end_ptr = add_map_value (end_ptr, number, string);
1272 c = read_skip_spaces ();
1273 }
1274 while (c != ']');
1275
1276 return m;
1277 }
1278
1279 /* For iterator with name ATTR_NAME generate define_attr with values
1280 'yes' and 'no'. This attribute is used to mark templates to which
1281 define_subst ATTR_NAME should be applied. This attribute is set and
1282 defined implicitly and automatically. */
1283 static void
1284 add_define_attr_for_define_subst (const char *attr_name, vec<rtx> *queue)
1285 {
1286 rtx const_str, return_rtx;
1287
1288 return_rtx = rtx_alloc (DEFINE_ATTR);
1289 PUT_CODE (return_rtx, DEFINE_ATTR);
1290
1291 const_str = rtx_alloc (CONST_STRING);
1292 PUT_CODE (const_str, CONST_STRING);
1293 XSTR (const_str, 0) = xstrdup ("no");
1294
1295 XSTR (return_rtx, 0) = xstrdup (attr_name);
1296 XSTR (return_rtx, 1) = xstrdup ("no,yes");
1297 XEXP (return_rtx, 2) = const_str;
1298
1299 queue->safe_push (return_rtx);
1300 }
1301
1302 /* This routine generates DEFINE_SUBST_ATTR expression with operands
1303 ATTR_OPERANDS and places it to QUEUE. */
1304 static void
1305 add_define_subst_attr (const char **attr_operands, vec<rtx> *queue)
1306 {
1307 rtx return_rtx;
1308 int i;
1309
1310 return_rtx = rtx_alloc (DEFINE_SUBST_ATTR);
1311 PUT_CODE (return_rtx, DEFINE_SUBST_ATTR);
1312
1313 for (i = 0; i < 4; i++)
1314 XSTR (return_rtx, i) = xstrdup (attr_operands[i]);
1315
1316 queue->safe_push (return_rtx);
1317 }
1318
1319 /* Read define_subst_attribute construction. It has next form:
1320 (define_subst_attribute <attribute_name> <iterator_name> <value1> <value2>)
1321 Attribute is substituted with value1 when no subst is applied and with
1322 value2 in the opposite case.
1323 Attributes are added to SUBST_ATTRS_TABLE.
1324 In case the iterator is encountered for the first time, it's added to
1325 SUBST_ITERS_TABLE. Also, implicit define_attr is generated. */
1326
1327 static void
1328 read_subst_mapping (htab_t subst_iters_table, htab_t subst_attrs_table,
1329 vec<rtx> *queue)
1330 {
1331 struct mapping *m;
1332 struct map_value **end_ptr;
1333 const char *attr_operands[4];
1334 int i;
1335
1336 for (i = 0; i < 4; i++)
1337 attr_operands[i] = rtx_reader_ptr->read_string (false);
1338
1339 add_define_subst_attr (attr_operands, queue);
1340
1341 bind_subst_iter_and_attr (attr_operands[1], attr_operands[0]);
1342
1343 m = (struct mapping *) htab_find (substs.iterators, &attr_operands[1]);
1344 if (!m)
1345 {
1346 m = add_mapping (&substs, subst_iters_table, attr_operands[1]);
1347 end_ptr = &m->values;
1348 end_ptr = add_map_value (end_ptr, 1, "");
1349 add_map_value (end_ptr, 2, "");
1350
1351 add_define_attr_for_define_subst (attr_operands[1], queue);
1352 }
1353
1354 m = add_mapping (&substs, subst_attrs_table, attr_operands[0]);
1355 end_ptr = &m->values;
1356 end_ptr = add_map_value (end_ptr, 1, attr_operands[2]);
1357 add_map_value (end_ptr, 2, attr_operands[3]);
1358 }
1359
1360 /* Check newly-created code iterator ITERATOR to see whether every code has the
1361 same format. */
1362
1363 static void
1364 check_code_iterator (struct mapping *iterator)
1365 {
1366 struct map_value *v;
1367 enum rtx_code bellwether;
1368
1369 bellwether = (enum rtx_code) iterator->values->number;
1370 for (v = iterator->values->next; v != 0; v = v->next)
1371 if (strcmp (GET_RTX_FORMAT (bellwether), GET_RTX_FORMAT (v->number)) != 0)
1372 fatal_with_file_and_line ("code iterator `%s' combines "
1373 "`%s' and `%s', which have different "
1374 "rtx formats", iterator->name,
1375 GET_RTX_NAME (bellwether),
1376 GET_RTX_NAME (v->number));
1377 }
1378
1379 /* Check that all values of attribute ATTR are rtx codes that have a
1380 consistent format. Return a representative code. */
1381
1382 static rtx_code
1383 check_code_attribute (mapping *attr)
1384 {
1385 rtx_code bellwether = UNKNOWN;
1386 for (map_value *v = attr->values; v != 0; v = v->next)
1387 {
1388 rtx_code code = maybe_find_code (v->string);
1389 if (code == UNKNOWN)
1390 fatal_with_file_and_line ("code attribute `%s' contains "
1391 "unrecognized rtx code `%s'",
1392 attr->name, v->string);
1393 if (bellwether == UNKNOWN)
1394 bellwether = code;
1395 else if (strcmp (GET_RTX_FORMAT (bellwether),
1396 GET_RTX_FORMAT (code)) != 0)
1397 fatal_with_file_and_line ("code attribute `%s' combines "
1398 "`%s' and `%s', which have different "
1399 "rtx formats", attr->name,
1400 GET_RTX_NAME (bellwether),
1401 GET_RTX_NAME (code));
1402 }
1403 return bellwether;
1404 }
1405
1406 /* Read an rtx-related declaration from the MD file, given that it
1407 starts with directive name RTX_NAME. Return true if it expands to
1408 one or more rtxes (as defined by rtx.def). When returning true,
1409 store the list of rtxes as an EXPR_LIST in *X. */
1410
1411 bool
1412 rtx_reader::read_rtx (const char *rtx_name, vec<rtx> *rtxen)
1413 {
1414 /* Handle various rtx-related declarations that aren't themselves
1415 encoded as rtxes. */
1416 if (strcmp (rtx_name, "define_conditions") == 0)
1417 {
1418 read_conditions ();
1419 return false;
1420 }
1421 if (strcmp (rtx_name, "define_mode_attr") == 0)
1422 {
1423 read_mapping (&modes, modes.attrs);
1424 return false;
1425 }
1426 if (strcmp (rtx_name, "define_mode_iterator") == 0)
1427 {
1428 read_mapping (&modes, modes.iterators);
1429 return false;
1430 }
1431 if (strcmp (rtx_name, "define_code_attr") == 0)
1432 {
1433 read_mapping (&codes, codes.attrs);
1434 return false;
1435 }
1436 if (strcmp (rtx_name, "define_code_iterator") == 0)
1437 {
1438 check_code_iterator (read_mapping (&codes, codes.iterators));
1439 return false;
1440 }
1441 if (strcmp (rtx_name, "define_int_attr") == 0)
1442 {
1443 read_mapping (&ints, ints.attrs);
1444 return false;
1445 }
1446 if (strcmp (rtx_name, "define_int_iterator") == 0)
1447 {
1448 read_mapping (&ints, ints.iterators);
1449 return false;
1450 }
1451 if (strcmp (rtx_name, "define_subst_attr") == 0)
1452 {
1453 read_subst_mapping (substs.iterators, substs.attrs, rtxen);
1454
1455 /* READ_SUBST_MAPPING could generate a new DEFINE_ATTR. Return
1456 TRUE to process it. */
1457 return true;
1458 }
1459
1460 apply_iterators (rtx_reader_ptr->read_rtx_code (rtx_name), rtxen);
1461 iterator_uses.truncate (0);
1462 attribute_uses.truncate (0);
1463
1464 return true;
1465 }
1466
1467 #endif /* #ifdef GENERATOR_FILE */
1468
1469 /* Do one-time initialization. */
1470
1471 static void
1472 one_time_initialization (void)
1473 {
1474 static bool initialized = false;
1475
1476 if (!initialized)
1477 {
1478 initialize_iterators ();
1479 initialized = true;
1480 }
1481 }
1482
1483 /* Consume characters until encountering a character in TERMINATOR_CHARS,
1484 consuming the terminator character if CONSUME_TERMINATOR is true.
1485 Return all characters before the terminator as an allocated buffer. */
1486
1487 char *
1488 rtx_reader::read_until (const char *terminator_chars, bool consume_terminator)
1489 {
1490 int ch = read_skip_spaces ();
1491 unread_char (ch);
1492 auto_vec<char> buf;
1493 while (1)
1494 {
1495 ch = read_char ();
1496 if (strchr (terminator_chars, ch))
1497 {
1498 if (!consume_terminator)
1499 unread_char (ch);
1500 break;
1501 }
1502 buf.safe_push (ch);
1503 }
1504 buf.safe_push ('\0');
1505 return xstrdup (buf.address ());
1506 }
1507
1508 /* Subroutine of read_rtx_code, for parsing zero or more flags. */
1509
1510 static void
1511 read_flags (rtx return_rtx)
1512 {
1513 while (1)
1514 {
1515 int ch = read_char ();
1516 if (ch != '/')
1517 {
1518 unread_char (ch);
1519 break;
1520 }
1521
1522 int flag_char = read_char ();
1523 switch (flag_char)
1524 {
1525 case 's':
1526 RTX_FLAG (return_rtx, in_struct) = 1;
1527 break;
1528 case 'v':
1529 RTX_FLAG (return_rtx, volatil) = 1;
1530 break;
1531 case 'u':
1532 RTX_FLAG (return_rtx, unchanging) = 1;
1533 break;
1534 case 'f':
1535 RTX_FLAG (return_rtx, frame_related) = 1;
1536 break;
1537 case 'j':
1538 RTX_FLAG (return_rtx, jump) = 1;
1539 break;
1540 case 'c':
1541 RTX_FLAG (return_rtx, call) = 1;
1542 break;
1543 case 'i':
1544 RTX_FLAG (return_rtx, return_val) = 1;
1545 break;
1546 default:
1547 fatal_with_file_and_line ("unrecognized flag: `%c'", flag_char);
1548 }
1549 }
1550 }
1551
1552 /* Return the numeric value n for GET_REG_NOTE_NAME (n) for STRING,
1553 or fail if STRING isn't recognized. */
1554
1555 static int
1556 parse_reg_note_name (const char *string)
1557 {
1558 for (int i = 0; i < REG_NOTE_MAX; i++)
1559 if (strcmp (string, GET_REG_NOTE_NAME (i)) == 0)
1560 return i;
1561 fatal_with_file_and_line ("unrecognized REG_NOTE name: `%s'", string);
1562 }
1563
1564 /* Allocate an rtx for code NAME. If NAME is a code iterator or code
1565 attribute, record its use for later and use one of its possible
1566 values as an interim rtx code. */
1567
1568 rtx
1569 rtx_reader::rtx_alloc_for_name (const char *name)
1570 {
1571 #ifdef GENERATOR_FILE
1572 size_t len = strlen (name);
1573 if (name[0] == '<' && name[len - 1] == '>')
1574 {
1575 /* Copy the attribute string into permanent storage, without the
1576 angle brackets around it. */
1577 obstack *strings = get_string_obstack ();
1578 obstack_grow0 (strings, name + 1, len - 2);
1579 char *deferred_name = XOBFINISH (strings, char *);
1580
1581 /* Find the name of the attribute. */
1582 const char *attr = strchr (deferred_name, ':');
1583 if (!attr)
1584 attr = deferred_name;
1585
1586 /* Find the attribute itself. */
1587 mapping *m = (mapping *) htab_find (codes.attrs, &attr);
1588 if (!m)
1589 fatal_with_file_and_line ("unknown code attribute `%s'", attr);
1590
1591 /* Pick the first possible code for now, and record the attribute
1592 use for later. */
1593 rtx x = rtx_alloc (check_code_attribute (m));
1594 record_attribute_use (&codes, get_current_location (),
1595 x, 0, deferred_name);
1596 return x;
1597 }
1598
1599 mapping *iterator = (mapping *) htab_find (codes.iterators, &name);
1600 if (iterator != 0)
1601 {
1602 /* Pick the first possible code for now, and record the iterator
1603 use for later. */
1604 rtx x = rtx_alloc (rtx_code (iterator->values->number));
1605 record_iterator_use (iterator, x, 0);
1606 return x;
1607 }
1608 #endif
1609
1610 return rtx_alloc (rtx_code (codes.find_builtin (name)));
1611 }
1612
1613 /* Subroutine of read_rtx and read_nested_rtx. CODE_NAME is the name of
1614 either an rtx code or a code iterator. Parse the rest of the rtx and
1615 return it. */
1616
1617 rtx
1618 rtx_reader::read_rtx_code (const char *code_name)
1619 {
1620 RTX_CODE code;
1621 const char *format_ptr;
1622 struct md_name name;
1623 rtx return_rtx;
1624 int c;
1625 long reuse_id = -1;
1626
1627 /* Linked list structure for making RTXs: */
1628 struct rtx_list
1629 {
1630 struct rtx_list *next;
1631 rtx value; /* Value of this node. */
1632 };
1633
1634 /* Handle reuse_rtx ids e.g. "(0|scratch:DI)". */
1635 if (ISDIGIT (code_name[0]))
1636 {
1637 reuse_id = atoi (code_name);
1638 while (char ch = *code_name++)
1639 if (ch == '|')
1640 break;
1641 }
1642
1643 /* Handle "reuse_rtx". */
1644 if (strcmp (code_name, "reuse_rtx") == 0)
1645 {
1646 read_name (&name);
1647 unsigned idx = atoi (name.string);
1648 /* Look it up by ID. */
1649 gcc_assert (idx < m_reuse_rtx_by_id.length ());
1650 return_rtx = m_reuse_rtx_by_id[idx];
1651 return return_rtx;
1652 }
1653
1654 /* Handle "const_double_zero". */
1655 if (strcmp (code_name, "const_double_zero") == 0)
1656 {
1657 code = CONST_DOUBLE;
1658 return_rtx = rtx_alloc (code);
1659 memset (return_rtx, 0, RTX_CODE_SIZE (code));
1660 PUT_CODE (return_rtx, code);
1661 c = read_skip_spaces ();
1662 if (c == ':')
1663 {
1664 file_location loc = read_name (&name);
1665 record_potential_iterator_use (&modes, loc, return_rtx, 0,
1666 name.string);
1667 }
1668 else
1669 unread_char (c);
1670 return return_rtx;
1671 }
1672
1673 /* If we end up with an insn expression then we free this space below. */
1674 return_rtx = rtx_alloc_for_name (code_name);
1675 code = GET_CODE (return_rtx);
1676 format_ptr = GET_RTX_FORMAT (code);
1677 memset (return_rtx, 0, RTX_CODE_SIZE (code));
1678 PUT_CODE (return_rtx, code);
1679
1680 if (reuse_id != -1)
1681 {
1682 /* Store away for later reuse. */
1683 m_reuse_rtx_by_id.safe_grow_cleared (reuse_id + 1, true);
1684 m_reuse_rtx_by_id[reuse_id] = return_rtx;
1685 }
1686
1687 /* Check for flags. */
1688 read_flags (return_rtx);
1689
1690 /* Read REG_NOTE names for EXPR_LIST and INSN_LIST. */
1691 if ((GET_CODE (return_rtx) == EXPR_LIST
1692 || GET_CODE (return_rtx) == INSN_LIST
1693 || GET_CODE (return_rtx) == INT_LIST)
1694 && !m_in_call_function_usage)
1695 {
1696 char ch = read_char ();
1697 if (ch == ':')
1698 {
1699 read_name (&name);
1700 PUT_MODE_RAW (return_rtx,
1701 (machine_mode)parse_reg_note_name (name.string));
1702 }
1703 else
1704 unread_char (ch);
1705 }
1706
1707 /* If what follows is `: mode ', read it and
1708 store the mode in the rtx. */
1709
1710 c = read_skip_spaces ();
1711 if (c == ':')
1712 {
1713 file_location loc = read_name (&name);
1714 record_potential_iterator_use (&modes, loc, return_rtx, 0, name.string);
1715 }
1716 else
1717 unread_char (c);
1718
1719 if (INSN_CHAIN_CODE_P (code))
1720 {
1721 read_name (&name);
1722 INSN_UID (return_rtx) = atoi (name.string);
1723 }
1724
1725 /* Use the format_ptr to parse the various operands of this rtx. */
1726 for (int idx = 0; format_ptr[idx] != 0; idx++)
1727 return_rtx = read_rtx_operand (return_rtx, idx);
1728
1729 /* Handle any additional information that after the regular fields
1730 (e.g. when parsing function dumps). */
1731 handle_any_trailing_information (return_rtx);
1732
1733 if (CONST_WIDE_INT_P (return_rtx))
1734 {
1735 read_name (&name);
1736 validate_const_wide_int (name.string);
1737 {
1738 const char *s = name.string;
1739 int len;
1740 int index = 0;
1741 int gs = HOST_BITS_PER_WIDE_INT/4;
1742 int pos;
1743 char * buf = XALLOCAVEC (char, gs + 1);
1744 unsigned HOST_WIDE_INT wi;
1745 int wlen;
1746
1747 /* Skip the leading spaces. */
1748 while (*s && ISSPACE (*s))
1749 s++;
1750
1751 /* Skip the leading 0x. */
1752 gcc_assert (s[0] == '0');
1753 gcc_assert (s[1] == 'x');
1754 s += 2;
1755
1756 len = strlen (s);
1757 pos = len - gs;
1758 wlen = (len + gs - 1) / gs; /* Number of words needed */
1759
1760 return_rtx = const_wide_int_alloc (wlen);
1761
1762 while (pos > 0)
1763 {
1764 #if HOST_BITS_PER_WIDE_INT == 64
1765 sscanf (s + pos, "%16" HOST_WIDE_INT_PRINT "x", &wi);
1766 #else
1767 sscanf (s + pos, "%8" HOST_WIDE_INT_PRINT "x", &wi);
1768 #endif
1769 CWI_ELT (return_rtx, index++) = wi;
1770 pos -= gs;
1771 }
1772 strncpy (buf, s, gs - pos);
1773 buf [gs - pos] = 0;
1774 sscanf (buf, "%" HOST_WIDE_INT_PRINT "x", &wi);
1775 CWI_ELT (return_rtx, index++) = wi;
1776 /* TODO: After reading, do we want to canonicalize with:
1777 value = lookup_const_wide_int (value); ? */
1778 }
1779 }
1780
1781 c = read_skip_spaces ();
1782 /* Syntactic sugar for AND and IOR, allowing Lisp-like
1783 arbitrary number of arguments for them. */
1784 if (c == '('
1785 && (GET_CODE (return_rtx) == AND
1786 || GET_CODE (return_rtx) == IOR))
1787 return read_rtx_variadic (return_rtx);
1788
1789 unread_char (c);
1790 return return_rtx;
1791 }
1792
1793 /* Subroutine of read_rtx_code. Parse operand IDX within RETURN_RTX,
1794 based on the corresponding format character within GET_RTX_FORMAT
1795 for the GET_CODE (RETURN_RTX), and return RETURN_RTX.
1796 This is a virtual function, so that function_reader can override
1797 some parsing, and potentially return a different rtx. */
1798
1799 rtx
1800 rtx_reader::read_rtx_operand (rtx return_rtx, int idx)
1801 {
1802 RTX_CODE code = GET_CODE (return_rtx);
1803 const char *format_ptr = GET_RTX_FORMAT (code);
1804 int c;
1805 struct md_name name;
1806
1807 switch (format_ptr[idx])
1808 {
1809 /* 0 means a field for internal use only.
1810 Don't expect it to be present in the input. */
1811 case '0':
1812 if (code == REG)
1813 ORIGINAL_REGNO (return_rtx) = REGNO (return_rtx);
1814 break;
1815
1816 case 'e':
1817 XEXP (return_rtx, idx) = read_nested_rtx ();
1818 break;
1819
1820 case 'u':
1821 XEXP (return_rtx, idx) = read_nested_rtx ();
1822 break;
1823
1824 case 'V':
1825 /* 'V' is an optional vector: if a closeparen follows,
1826 just store NULL for this element. */
1827 c = read_skip_spaces ();
1828 unread_char (c);
1829 if (c == ')')
1830 {
1831 XVEC (return_rtx, idx) = 0;
1832 break;
1833 }
1834 /* Now process the vector. */
1835 /* FALLTHRU */
1836
1837 case 'E':
1838 {
1839 /* Obstack to store scratch vector in. */
1840 struct obstack vector_stack;
1841 int list_counter = 0;
1842 rtvec return_vec = NULL_RTVEC;
1843 rtx saved_rtx = NULL_RTX;
1844
1845 require_char_ws ('[');
1846
1847 /* Add expressions to a list, while keeping a count. */
1848 obstack_init (&vector_stack);
1849 while ((c = read_skip_spaces ()) && c != ']')
1850 {
1851 if (c == EOF)
1852 fatal_expected_char (']', c);
1853 unread_char (c);
1854
1855 rtx value;
1856 int repeat_count = 1;
1857 if (c == 'r')
1858 {
1859 /* Process "repeated xN" directive. */
1860 read_name (&name);
1861 if (strcmp (name.string, "repeated"))
1862 fatal_with_file_and_line ("invalid directive \"%s\"\n",
1863 name.string);
1864 read_name (&name);
1865 if (!sscanf (name.string, "x%d", &repeat_count))
1866 fatal_with_file_and_line ("invalid repeat count \"%s\"\n",
1867 name.string);
1868
1869 /* We already saw one of the instances. */
1870 repeat_count--;
1871 value = saved_rtx;
1872 }
1873 else
1874 value = read_nested_rtx ();
1875
1876 for (; repeat_count > 0; repeat_count--)
1877 {
1878 list_counter++;
1879 obstack_ptr_grow (&vector_stack, value);
1880 }
1881 saved_rtx = value;
1882 }
1883 if (list_counter > 0)
1884 {
1885 return_vec = rtvec_alloc (list_counter);
1886 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
1887 list_counter * sizeof (rtx));
1888 }
1889 else if (format_ptr[idx] == 'E')
1890 fatal_with_file_and_line ("vector must have at least one element");
1891 XVEC (return_rtx, idx) = return_vec;
1892 obstack_free (&vector_stack, NULL);
1893 /* close bracket gotten */
1894 }
1895 break;
1896
1897 case 'S':
1898 case 'T':
1899 case 's':
1900 {
1901 char *stringbuf;
1902 int star_if_braced;
1903
1904 c = read_skip_spaces ();
1905 unread_char (c);
1906 if (c == ')')
1907 {
1908 /* 'S' fields are optional and should be NULL if no string
1909 was given. Also allow normal 's' and 'T' strings to be
1910 omitted, treating them in the same way as empty strings. */
1911 XSTR (return_rtx, idx) = (format_ptr[idx] == 'S' ? NULL : "");
1912 break;
1913 }
1914
1915 /* The output template slot of a DEFINE_INSN, DEFINE_INSN_AND_SPLIT,
1916 DEFINE_INSN_AND_REWRITE or DEFINE_PEEPHOLE automatically
1917 gets a star inserted as its first character, if it is
1918 written with a brace block instead of a string constant. */
1919 star_if_braced = (format_ptr[idx] == 'T');
1920
1921 stringbuf = read_string (star_if_braced);
1922 if (!stringbuf)
1923 break;
1924
1925 #ifdef GENERATOR_FILE
1926 /* For insn patterns, we want to provide a default name
1927 based on the file and line, like "*foo.md:12", if the
1928 given name is blank. These are only for define_insn and
1929 define_insn_and_split, to aid debugging. */
1930 if (*stringbuf == '\0'
1931 && idx == 0
1932 && (GET_CODE (return_rtx) == DEFINE_INSN
1933 || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT
1934 || GET_CODE (return_rtx) == DEFINE_INSN_AND_REWRITE))
1935 {
1936 const char *old_stringbuf = stringbuf;
1937 struct obstack *string_obstack = get_string_obstack ();
1938 char line_name[20];
1939 const char *read_md_filename = get_filename ();
1940 const char *fn = (read_md_filename ? read_md_filename : "rtx");
1941 const char *slash;
1942 for (slash = fn; *slash; slash ++)
1943 if (*slash == '/' || *slash == '\\' || *slash == ':')
1944 fn = slash + 1;
1945 obstack_1grow (string_obstack, '*');
1946 obstack_grow (string_obstack, fn, strlen (fn));
1947 sprintf (line_name, ":%d", get_lineno ());
1948 obstack_grow (string_obstack, line_name, strlen (line_name)+1);
1949 stringbuf = XOBFINISH (string_obstack, char *);
1950 copy_md_ptr_loc (stringbuf, old_stringbuf);
1951 }
1952
1953 /* Find attr-names in the string. */
1954 char *str;
1955 char *start, *end, *ptr;
1956 char tmpstr[256];
1957 ptr = &tmpstr[0];
1958 end = stringbuf;
1959 while ((start = strchr (end, '<')) && (end = strchr (start, '>')))
1960 {
1961 if ((end - start - 1 > 0)
1962 && (end - start - 1 < (int)sizeof (tmpstr)))
1963 {
1964 strncpy (tmpstr, start+1, end-start-1);
1965 tmpstr[end-start-1] = 0;
1966 end++;
1967 }
1968 else
1969 break;
1970 struct mapping *m
1971 = (struct mapping *) htab_find (substs.attrs, &ptr);
1972 if (m != 0)
1973 {
1974 /* Here we should find linked subst-iter. */
1975 str = find_subst_iter_by_attr (ptr);
1976 if (str)
1977 m = (struct mapping *) htab_find (substs.iterators, &str);
1978 else
1979 m = 0;
1980 }
1981 if (m != 0)
1982 record_iterator_use (m, return_rtx, 0);
1983 }
1984 #endif /* #ifdef GENERATOR_FILE */
1985
1986 const char *string_ptr = finalize_string (stringbuf);
1987
1988 if (star_if_braced)
1989 XTMPL (return_rtx, idx) = string_ptr;
1990 else
1991 XSTR (return_rtx, idx) = string_ptr;
1992 }
1993 break;
1994
1995 case 'i':
1996 case 'n':
1997 case 'w':
1998 case 'p':
1999 {
2000 /* Can be an iterator or an integer constant. */
2001 file_location loc = read_name (&name);
2002 record_potential_iterator_use (&ints, loc, return_rtx, idx,
2003 name.string);
2004 break;
2005 }
2006
2007 case 'r':
2008 read_name (&name);
2009 validate_const_int (name.string);
2010 set_regno_raw (return_rtx, atoi (name.string), 1);
2011 REG_ATTRS (return_rtx) = NULL;
2012 break;
2013
2014 default:
2015 gcc_unreachable ();
2016 }
2017
2018 return return_rtx;
2019 }
2020
2021 /* Read a nested rtx construct from the MD file and return it. */
2022
2023 rtx
2024 rtx_reader::read_nested_rtx ()
2025 {
2026 struct md_name name;
2027 rtx return_rtx;
2028
2029 /* In compact dumps, trailing "(nil)" values can be omitted.
2030 Handle such dumps. */
2031 if (peek_char () == ')')
2032 return NULL_RTX;
2033
2034 require_char_ws ('(');
2035
2036 read_name (&name);
2037 if (strcmp (name.string, "nil") == 0)
2038 return_rtx = NULL;
2039 else
2040 return_rtx = read_rtx_code (name.string);
2041
2042 require_char_ws (')');
2043
2044 return_rtx = postprocess (return_rtx);
2045
2046 return return_rtx;
2047 }
2048
2049 /* Mutually recursive subroutine of read_rtx which reads
2050 (thing x1 x2 x3 ...) and produces RTL as if
2051 (thing x1 (thing x2 (thing x3 ...))) had been written.
2052 When called, FORM is (thing x1 x2), and the file position
2053 is just past the leading parenthesis of x3. Only works
2054 for THINGs which are dyadic expressions, e.g. AND, IOR. */
2055 rtx
2056 rtx_reader::read_rtx_variadic (rtx form)
2057 {
2058 char c = '(';
2059 rtx p = form, q;
2060
2061 do
2062 {
2063 unread_char (c);
2064
2065 q = rtx_alloc (GET_CODE (p));
2066 PUT_MODE (q, GET_MODE (p));
2067
2068 XEXP (q, 0) = XEXP (p, 1);
2069 XEXP (q, 1) = read_nested_rtx ();
2070
2071 XEXP (p, 1) = q;
2072 p = q;
2073 c = read_skip_spaces ();
2074 }
2075 while (c == '(');
2076 unread_char (c);
2077 return form;
2078 }
2079
2080 /* Constructor for class rtx_reader. */
2081
2082 rtx_reader::rtx_reader (bool compact)
2083 : md_reader (compact),
2084 m_in_call_function_usage (false)
2085 {
2086 /* Set the global singleton pointer. */
2087 rtx_reader_ptr = this;
2088
2089 one_time_initialization ();
2090 }
2091
2092 /* Destructor for class rtx_reader. */
2093
2094 rtx_reader::~rtx_reader ()
2095 {
2096 /* Clear the global singleton pointer. */
2097 rtx_reader_ptr = NULL;
2098 }