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

source code of gcc/read-rtl.cc