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