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