OSDN Git Service

gcc/
[pf3gnuchains/gcc-fork.git] / gcc / read-rtl.c
1 /* RTL reader for GCC.
2    Copyright (C) 1987, 1988, 1991, 1994, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2007, 2008
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "bconfig.h"
23
24 /* Disable rtl checking; it conflicts with the iterator handling.  */
25 #undef ENABLE_RTL_CHECKING
26
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "rtl.h"
31 #include "obstack.h"
32 #include "hashtab.h"
33 #include "read-md.h"
34 #include "gensupport.h"
35
36 /* One element in a singly-linked list of (integer, string) pairs.  */
37 struct map_value {
38   struct map_value *next;
39   int number;
40   const char *string;
41 };
42
43 /* Maps an iterator or attribute name to a list of (integer, string) pairs.
44    The integers are mode or code values; the strings are either C conditions
45    or attribute values.  */
46 struct mapping {
47   /* The name of the iterator or attribute.  */
48   const char *name;
49
50   /* The group (modes or codes) to which the iterator or attribute belongs.  */
51   struct iterator_group *group;
52
53   /* Gives a unique number to the attribute or iterator.  Numbers are
54      allocated consecutively, starting at 0.  */
55   int index;
56
57   /* The list of (integer, string) pairs.  */
58   struct map_value *values;
59 };
60
61 /* A structure for abstracting the common parts of code and mode iterators.  */
62 struct iterator_group {
63   /* Tables of "mapping" structures, one for attributes and one for iterators.  */
64   htab_t attrs, iterators;
65
66   /* The number of "real" modes or codes (and by extension, the first
67      number available for use as an iterator placeholder).  */
68   int num_builtins;
69
70   /* Treat the given string as the name of a standard mode or code and
71      return its integer value.  */
72   int (*find_builtin) (const char *);
73
74   /* Return true if the given rtx uses the given mode or code.  */
75   bool (*uses_iterator_p) (rtx, int);
76
77   /* Make the given rtx use the given mode or code.  */
78   void (*apply_iterator) (rtx, int);
79 };
80
81 /* A structure used to pass data from read_rtx to apply_iterator_traverse
82    via htab_traverse.  */
83 struct iterator_traverse_data {
84   /* Instruction queue.  */
85   rtx queue;
86   /* Attributes seen for modes.  */
87   struct map_value *mode_maps;
88   /* The last unknown attribute used as a mode.  */
89   const char *unknown_mode_attr;
90 };
91
92 /* If CODE is the number of a code iterator, return a real rtx code that
93    has the same format.  Return CODE otherwise.  */
94 #define BELLWETHER_CODE(CODE) \
95   ((CODE) < NUM_RTX_CODE ? CODE : bellwether_codes[CODE - NUM_RTX_CODE])
96
97 static int find_mode (const char *);
98 static bool uses_mode_iterator_p (rtx, int);
99 static void apply_mode_iterator (rtx, int);
100 static int find_code (const char *);
101 static bool uses_code_iterator_p (rtx, int);
102 static void apply_code_iterator (rtx, int);
103 static const char *apply_iterator_to_string (const char *, struct mapping *, int);
104 static rtx apply_iterator_to_rtx (rtx, struct mapping *, int,
105                                   struct map_value *, const char **);
106 static bool uses_iterator_p (rtx, struct mapping *);
107 static const char *add_condition_to_string (const char *, const char *);
108 static void add_condition_to_rtx (rtx, const char *);
109 static int apply_iterator_traverse (void **, void *);
110 static struct mapping *add_mapping (struct iterator_group *, htab_t t,
111                                     const char *);
112 static struct map_value **add_map_value (struct map_value **,
113                                          int, const char *);
114 static void initialize_iterators (void);
115 static void read_conditions (void);
116 static void validate_const_int (const char *);
117 static int find_iterator (struct iterator_group *, const char *);
118 static struct mapping *read_mapping (struct iterator_group *, htab_t);
119 static void check_code_iterator (struct mapping *);
120 static rtx read_rtx_1 (struct map_value **);
121 static rtx read_rtx_variadic (struct map_value **, rtx);
122
123 /* The mode and code iterator structures.  */
124 static struct iterator_group modes, codes;
125
126 /* Index I is the value of BELLWETHER_CODE (I + NUM_RTX_CODE).  */
127 static enum rtx_code *bellwether_codes;
128
129 /* Implementations of the iterator_group callbacks for modes.  */
130
131 static int
132 find_mode (const char *name)
133 {
134   int i;
135
136   for (i = 0; i < NUM_MACHINE_MODES; i++)
137     if (strcmp (GET_MODE_NAME (i), name) == 0)
138       return i;
139
140   fatal_with_file_and_line ("unknown mode `%s'", name);
141 }
142
143 static bool
144 uses_mode_iterator_p (rtx x, int mode)
145 {
146   return (int) GET_MODE (x) == mode;
147 }
148
149 static void
150 apply_mode_iterator (rtx x, int mode)
151 {
152   PUT_MODE (x, (enum machine_mode) mode);
153 }
154
155 /* Implementations of the iterator_group callbacks for codes.  */
156
157 static int
158 find_code (const char *name)
159 {
160   int i;
161
162   for (i = 0; i < NUM_RTX_CODE; i++)
163     if (strcmp (GET_RTX_NAME (i), name) == 0)
164       return i;
165
166   fatal_with_file_and_line ("unknown rtx code `%s'", name);
167 }
168
169 static bool
170 uses_code_iterator_p (rtx x, int code)
171 {
172   return (int) GET_CODE (x) == code;
173 }
174
175 static void
176 apply_code_iterator (rtx x, int code)
177 {
178   PUT_CODE (x, (enum rtx_code) code);
179 }
180
181 /* Map a code or mode attribute string P to the underlying string for
182    ITERATOR and VALUE.  */
183
184 static struct map_value *
185 map_attr_string (const char *p, struct mapping *iterator, int value)
186 {
187   const char *attr;
188   struct mapping *m;
189   struct map_value *v;
190
191   /* If there's a "iterator:" prefix, check whether the iterator name matches.
192      Set ATTR to the start of the attribute name.  */
193   attr = strchr (p, ':');
194   if (attr == 0)
195     attr = p;
196   else
197     {
198       if (strncmp (p, iterator->name, attr - p) != 0
199           || iterator->name[attr - p] != 0)
200         return 0;
201       attr++;
202     }
203
204   /* Find the attribute specification.  */
205   m = (struct mapping *) htab_find (iterator->group->attrs, &attr);
206   if (m == 0)
207     return 0;
208
209   /* Find the attribute value for VALUE.  */
210   for (v = m->values; v != 0; v = v->next)
211     if (v->number == value)
212       break;
213
214   return v;
215 }
216
217 /* Given an attribute string used as a machine mode, return an index
218    to store in the machine mode to be translated by
219    apply_iterator_to_rtx.  */
220
221 static unsigned int
222 mode_attr_index (struct map_value **mode_maps, const char *string)
223 {
224   char *p;
225   struct map_value *mv;
226
227   /* Copy the attribute string into permanent storage, without the
228      angle brackets around it.  */
229   obstack_grow0 (&string_obstack, string + 1, strlen (string) - 2);
230   p = XOBFINISH (&string_obstack, char *);
231
232   mv = XNEW (struct map_value);
233   mv->number = *mode_maps == 0 ? 0 : (*mode_maps)->number + 1;
234   mv->string = p;
235   mv->next = *mode_maps;
236   *mode_maps = mv;
237
238   /* We return a code which we can map back into this string: the
239      number of machine modes + the number of mode iterators + the index
240      we just used.  */
241   return MAX_MACHINE_MODE + htab_elements (modes.iterators) + mv->number;
242 }
243
244 /* Apply MODE_MAPS to the top level of X, expanding cases where an
245    attribute is used for a mode.  ITERATOR is the current iterator we are
246    expanding, and VALUE is the value to which we are expanding it.
247    This sets *UNKNOWN to true if we find a mode attribute which has not
248    yet been defined, and does not change it otherwise.  */
249
250 static void
251 apply_mode_maps (rtx x, struct map_value *mode_maps, struct mapping *iterator,
252                  int value, const char **unknown)
253 {
254   unsigned int offset;
255   int indx;
256   struct map_value *pm;
257
258   offset = MAX_MACHINE_MODE + htab_elements (modes.iterators);
259   if (GET_MODE (x) < offset)
260     return;
261
262   indx = GET_MODE (x) - offset;
263   for (pm = mode_maps; pm; pm = pm->next)
264     {
265       if (pm->number == indx)
266         {
267           struct map_value *v;
268
269           v = map_attr_string (pm->string, iterator, value);
270           if (v)
271             PUT_MODE (x, (enum machine_mode) find_mode (v->string));
272           else
273             *unknown = pm->string;
274           return;
275         }
276     }
277 }
278
279 /* Given that ITERATOR is being expanded as VALUE, apply the appropriate
280    string substitutions to STRING.  Return the new string if any changes
281    were needed, otherwise return STRING itself.  */
282
283 static const char *
284 apply_iterator_to_string (const char *string, struct mapping *iterator, int value)
285 {
286   char *base, *copy, *p, *start, *end;
287   struct map_value *v;
288
289   if (string == 0)
290     return string;
291
292   base = p = copy = ASTRDUP (string);
293   while ((start = strchr (p, '<')) && (end = strchr (start, '>')))
294     {
295       p = start + 1;
296
297       *end = 0;
298       v = map_attr_string (p, iterator, value);
299       *end = '>';
300       if (v == 0)
301         continue;
302
303       /* Add everything between the last copied byte and the '<',
304          then add in the attribute value.  */
305       obstack_grow (&string_obstack, base, start - base);
306       obstack_grow (&string_obstack, v->string, strlen (v->string));
307       base = end + 1;
308     }
309   if (base != copy)
310     {
311       obstack_grow (&string_obstack, base, strlen (base) + 1);
312       copy = XOBFINISH (&string_obstack, char *);
313       copy_md_ptr_loc (copy, string);
314       return copy;
315     }
316   return string;
317 }
318
319 /* Return a copy of ORIGINAL in which all uses of ITERATOR have been
320    replaced by VALUE.  MODE_MAPS holds information about attribute
321    strings used for modes.  This sets *UNKNOWN_MODE_ATTR to the value of
322    an unknown mode attribute, and does not change it otherwise.  */
323
324 static rtx
325 apply_iterator_to_rtx (rtx original, struct mapping *iterator, int value,
326                        struct map_value *mode_maps,
327                        const char **unknown_mode_attr)
328 {
329   struct iterator_group *group;
330   const char *format_ptr;
331   int i, j;
332   rtx x;
333   enum rtx_code bellwether_code;
334
335   if (original == 0)
336     return original;
337
338   /* Create a shallow copy of ORIGINAL.  */
339   bellwether_code = BELLWETHER_CODE (GET_CODE (original));
340   x = rtx_alloc (bellwether_code);
341   memcpy (x, original, RTX_CODE_SIZE (bellwether_code));
342
343   /* Change the mode or code itself.  */
344   group = iterator->group;
345   if (group->uses_iterator_p (x, iterator->index + group->num_builtins))
346     group->apply_iterator (x, value);
347
348   if (mode_maps)
349     apply_mode_maps (x, mode_maps, iterator, value, unknown_mode_attr);
350
351   /* Change each string and recursively change each rtx.  */
352   format_ptr = GET_RTX_FORMAT (bellwether_code);
353   for (i = 0; format_ptr[i] != 0; i++)
354     switch (format_ptr[i])
355       {
356       case 'T':
357         XTMPL (x, i) = apply_iterator_to_string (XTMPL (x, i), iterator, value);
358         break;
359
360       case 'S':
361       case 's':
362         XSTR (x, i) = apply_iterator_to_string (XSTR (x, i), iterator, value);
363         break;
364
365       case 'e':
366         XEXP (x, i) = apply_iterator_to_rtx (XEXP (x, i), iterator, value,
367                                              mode_maps, unknown_mode_attr);
368         break;
369
370       case 'V':
371       case 'E':
372         if (XVEC (original, i))
373           {
374             XVEC (x, i) = rtvec_alloc (XVECLEN (original, i));
375             for (j = 0; j < XVECLEN (x, i); j++)
376               XVECEXP (x, i, j) = apply_iterator_to_rtx (XVECEXP (original, i, j),
377                                                          iterator, value, mode_maps,
378                                                          unknown_mode_attr);
379           }
380         break;
381
382       default:
383         break;
384       }
385   return x;
386 }
387
388 /* Return true if X (or some subexpression of X) uses iterator ITERATOR.  */
389
390 static bool
391 uses_iterator_p (rtx x, struct mapping *iterator)
392 {
393   struct iterator_group *group;
394   const char *format_ptr;
395   int i, j;
396
397   if (x == 0)
398     return false;
399
400   group = iterator->group;
401   if (group->uses_iterator_p (x, iterator->index + group->num_builtins))
402     return true;
403
404   format_ptr = GET_RTX_FORMAT (BELLWETHER_CODE (GET_CODE (x)));
405   for (i = 0; format_ptr[i] != 0; i++)
406     switch (format_ptr[i])
407       {
408       case 'e':
409         if (uses_iterator_p (XEXP (x, i), iterator))
410           return true;
411         break;
412
413       case 'V':
414       case 'E':
415         if (XVEC (x, i))
416           for (j = 0; j < XVECLEN (x, i); j++)
417             if (uses_iterator_p (XVECEXP (x, i, j), iterator))
418               return true;
419         break;
420
421       default:
422         break;
423       }
424   return false;
425 }
426
427 /* Return a condition that must satisfy both ORIGINAL and EXTRA.  If ORIGINAL
428    has the form "&& ..." (as used in define_insn_and_splits), assume that
429    EXTRA is already satisfied.  Empty strings are treated like "true".  */
430
431 static const char *
432 add_condition_to_string (const char *original, const char *extra)
433 {
434   if (original != 0 && original[0] == '&' && original[1] == '&')
435     return original;
436   return join_c_conditions (original, extra);
437 }
438
439 /* Like add_condition, but applied to all conditions in rtx X.  */
440
441 static void
442 add_condition_to_rtx (rtx x, const char *extra)
443 {
444   switch (GET_CODE (x))
445     {
446     case DEFINE_INSN:
447     case DEFINE_EXPAND:
448       XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
449       break;
450
451     case DEFINE_SPLIT:
452     case DEFINE_PEEPHOLE:
453     case DEFINE_PEEPHOLE2:
454     case DEFINE_COND_EXEC:
455       XSTR (x, 1) = add_condition_to_string (XSTR (x, 1), extra);
456       break;
457
458     case DEFINE_INSN_AND_SPLIT:
459       XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
460       XSTR (x, 4) = add_condition_to_string (XSTR (x, 4), extra);
461       break;
462
463     default:
464       break;
465     }
466 }
467
468 /* A htab_traverse callback.  Search the EXPR_LIST given by DATA
469    for rtxes that use the iterator in *SLOT.  Replace each such rtx
470    with a list of expansions.  */
471
472 static int
473 apply_iterator_traverse (void **slot, void *data)
474 {
475   struct iterator_traverse_data *mtd = (struct iterator_traverse_data *) data;
476   struct mapping *iterator;
477   struct map_value *v;
478   rtx elem, new_elem, original, x;
479
480   iterator = (struct mapping *) *slot;
481   for (elem = mtd->queue; elem != 0; elem = XEXP (elem, 1))
482     if (uses_iterator_p (XEXP (elem, 0), iterator))
483       {
484         /* For each iterator we expand, we set UNKNOWN_MODE_ATTR to NULL.
485            If apply_iterator_rtx finds an unknown attribute for a mode,
486            it will set it to the attribute.  We want to know whether
487            the attribute is unknown after we have expanded all
488            possible iterators, so setting it to NULL here gives us the
489            right result when the hash table traversal is complete.  */
490         mtd->unknown_mode_attr = NULL;
491
492         original = XEXP (elem, 0);
493         for (v = iterator->values; v != 0; v = v->next)
494           {
495             x = apply_iterator_to_rtx (original, iterator, v->number,
496                                        mtd->mode_maps,
497                                        &mtd->unknown_mode_attr);
498             add_condition_to_rtx (x, v->string);
499             if (v != iterator->values)
500               {
501                 /* Insert a new EXPR_LIST node after ELEM and put the
502                    new expansion there.  */
503                 new_elem = rtx_alloc (EXPR_LIST);
504                 XEXP (new_elem, 1) = XEXP (elem, 1);
505                 XEXP (elem, 1) = new_elem;
506                 elem = new_elem;
507               }
508             XEXP (elem, 0) = x;
509           }
510     }
511   return 1;
512 }
513
514 /* Add a new "mapping" structure to hashtable TABLE.  NAME is the name
515    of the mapping and GROUP is the group to which it belongs.  */
516
517 static struct mapping *
518 add_mapping (struct iterator_group *group, htab_t table, const char *name)
519 {
520   struct mapping *m;
521   void **slot;
522
523   m = XNEW (struct mapping);
524   m->name = xstrdup (name);
525   m->group = group;
526   m->index = htab_elements (table);
527   m->values = 0;
528
529   slot = htab_find_slot (table, m, INSERT);
530   if (*slot != 0)
531     fatal_with_file_and_line ("`%s' already defined", name);
532
533   *slot = m;
534   return m;
535 }
536
537 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
538    END_PTR points to the current null terminator for the list; return
539    a pointer the new null terminator.  */
540
541 static struct map_value **
542 add_map_value (struct map_value **end_ptr, int number, const char *string)
543 {
544   struct map_value *value;
545
546   value = XNEW (struct map_value);
547   value->next = 0;
548   value->number = number;
549   value->string = string;
550
551   *end_ptr = value;
552   return &value->next;
553 }
554
555 /* Do one-time initialization of the mode and code attributes.  */
556
557 static void
558 initialize_iterators (void)
559 {
560   struct mapping *lower, *upper;
561   struct map_value **lower_ptr, **upper_ptr;
562   char *copy, *p;
563   int i;
564
565   modes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
566   modes.iterators = htab_create (13, leading_string_hash,
567                                  leading_string_eq_p, 0);
568   modes.num_builtins = MAX_MACHINE_MODE;
569   modes.find_builtin = find_mode;
570   modes.uses_iterator_p = uses_mode_iterator_p;
571   modes.apply_iterator = apply_mode_iterator;
572
573   codes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
574   codes.iterators = htab_create (13, leading_string_hash,
575                                  leading_string_eq_p, 0);
576   codes.num_builtins = NUM_RTX_CODE;
577   codes.find_builtin = find_code;
578   codes.uses_iterator_p = uses_code_iterator_p;
579   codes.apply_iterator = apply_code_iterator;
580
581   lower = add_mapping (&modes, modes.attrs, "mode");
582   upper = add_mapping (&modes, modes.attrs, "MODE");
583   lower_ptr = &lower->values;
584   upper_ptr = &upper->values;
585   for (i = 0; i < MAX_MACHINE_MODE; i++)
586     {
587       copy = xstrdup (GET_MODE_NAME (i));
588       for (p = copy; *p != 0; p++)
589         *p = TOLOWER (*p);
590
591       upper_ptr = add_map_value (upper_ptr, i, GET_MODE_NAME (i));
592       lower_ptr = add_map_value (lower_ptr, i, copy);
593     }
594
595   lower = add_mapping (&codes, codes.attrs, "code");
596   upper = add_mapping (&codes, codes.attrs, "CODE");
597   lower_ptr = &lower->values;
598   upper_ptr = &upper->values;
599   for (i = 0; i < NUM_RTX_CODE; i++)
600     {
601       copy = xstrdup (GET_RTX_NAME (i));
602       for (p = copy; *p != 0; p++)
603         *p = TOUPPER (*p);
604
605       lower_ptr = add_map_value (lower_ptr, i, GET_RTX_NAME (i));
606       upper_ptr = add_map_value (upper_ptr, i, copy);
607     }
608 }
609 \f
610 /* Provide a version of a function to read a long long if the system does
611    not provide one.  */
612 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !defined(HAVE_ATOLL) && !defined(HAVE_ATOQ)
613 HOST_WIDE_INT atoll (const char *);
614
615 HOST_WIDE_INT
616 atoll (const char *p)
617 {
618   int neg = 0;
619   HOST_WIDE_INT tmp_wide;
620
621   while (ISSPACE (*p))
622     p++;
623   if (*p == '-')
624     neg = 1, p++;
625   else if (*p == '+')
626     p++;
627
628   tmp_wide = 0;
629   while (ISDIGIT (*p))
630     {
631       HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
632       if (new_wide < tmp_wide)
633         {
634           /* Return INT_MAX equiv on overflow.  */
635           tmp_wide = (~(unsigned HOST_WIDE_INT) 0) >> 1;
636           break;
637         }
638       tmp_wide = new_wide;
639       p++;
640     }
641
642   if (neg)
643     tmp_wide = -tmp_wide;
644   return tmp_wide;
645 }
646 #endif
647 \f
648 /* Process a define_conditions directive, starting with the optional
649    space after the "define_conditions".  The directive looks like this:
650
651      (define_conditions [
652         (number "string")
653         (number "string")
654         ...
655      ])
656
657    It's not intended to appear in machine descriptions.  It is
658    generated by (the program generated by) genconditions.c, and
659    slipped in at the beginning of the sequence of MD files read by
660    most of the other generators.  */
661 static void
662 read_conditions (void)
663 {
664   int c;
665
666   c = read_skip_spaces ();
667   if (c != '[')
668     fatal_expected_char ('[', c);
669
670   while ( (c = read_skip_spaces ()) != ']')
671     {
672       struct md_name name;
673       char *expr;
674       int value;
675
676       if (c != '(')
677         fatal_expected_char ('(', c);
678
679       read_name (&name);
680       validate_const_int (name.string);
681       value = atoi (name.string);
682
683       c = read_skip_spaces ();
684       if (c != '"')
685         fatal_expected_char ('"', c);
686       expr = read_quoted_string ();
687
688       c = read_skip_spaces ();
689       if (c != ')')
690         fatal_expected_char (')', c);
691
692       add_c_test (expr, value);
693     }
694   c = read_skip_spaces ();
695   if (c != ')')
696     fatal_expected_char (')', c);
697 }
698
699 static void
700 validate_const_int (const char *string)
701 {
702   const char *cp;
703   int valid = 1;
704
705   cp = string;
706   while (*cp && ISSPACE (*cp))
707     cp++;
708   if (*cp == '-' || *cp == '+')
709     cp++;
710   if (*cp == 0)
711     valid = 0;
712   for (; *cp; cp++)
713     if (! ISDIGIT (*cp))
714       valid = 0;
715   if (!valid)
716     fatal_with_file_and_line ("invalid decimal constant \"%s\"\n", string);
717 }
718
719 /* Search GROUP for a mode or code called NAME and return its numerical
720    identifier.  */
721
722 static int
723 find_iterator (struct iterator_group *group, const char *name)
724 {
725   struct mapping *m;
726
727   m = (struct mapping *) htab_find (group->iterators, &name);
728   if (m != 0)
729     return m->index + group->num_builtins;
730   return group->find_builtin (name);
731 }
732
733 /* Finish reading a declaration of the form:
734
735        (define... <name> [<value1> ... <valuen>])
736
737    from the MD file, where each <valuei> is either a bare symbol name or a
738    "(<name> <string>)" pair.  The "(define..." part has already been read.
739
740    Represent the declaration as a "mapping" structure; add it to TABLE
741    (which belongs to GROUP) and return it.  */
742
743 static struct mapping *
744 read_mapping (struct iterator_group *group, htab_t table)
745 {
746   struct md_name name;
747   struct mapping *m;
748   struct map_value **end_ptr;
749   const char *string;
750   int number, c;
751
752   /* Read the mapping name and create a structure for it.  */
753   read_name (&name);
754   m = add_mapping (group, table, name.string);
755
756   c = read_skip_spaces ();
757   if (c != '[')
758     fatal_expected_char ('[', c);
759
760   /* Read each value.  */
761   end_ptr = &m->values;
762   c = read_skip_spaces ();
763   do
764     {
765       if (c != '(')
766         {
767           /* A bare symbol name that is implicitly paired to an
768              empty string.  */
769           unread_char (c);
770           read_name (&name);
771           string = "";
772         }
773       else
774         {
775           /* A "(name string)" pair.  */
776           read_name (&name);
777           string = read_string (false);
778           c = read_skip_spaces ();
779           if (c != ')')
780             fatal_expected_char (')', c);
781         }
782       number = group->find_builtin (name.string);
783       end_ptr = add_map_value (end_ptr, number, string);
784       c = read_skip_spaces ();
785     }
786   while (c != ']');
787
788   c = read_skip_spaces ();
789   if (c != ')')
790     fatal_expected_char (')', c);
791
792   return m;
793 }
794
795 /* Check newly-created code iterator ITERATOR to see whether every code has the
796    same format.  Initialize the iterator's entry in bellwether_codes.  */
797
798 static void
799 check_code_iterator (struct mapping *iterator)
800 {
801   struct map_value *v;
802   enum rtx_code bellwether;
803
804   bellwether = (enum rtx_code) iterator->values->number;
805   for (v = iterator->values->next; v != 0; v = v->next)
806     if (strcmp (GET_RTX_FORMAT (bellwether), GET_RTX_FORMAT (v->number)) != 0)
807       fatal_with_file_and_line ("code iterator `%s' combines "
808                                 "different rtx formats", iterator->name);
809
810   bellwether_codes = XRESIZEVEC (enum rtx_code, bellwether_codes,
811                                  iterator->index + 1);
812   bellwether_codes[iterator->index] = bellwether;
813 }
814
815 /* Read an rtx in printed representation from the MD file and store
816    its core representation in *X.  Also store the line number of the
817    opening '(' in *LINENO.  Return true on success or false if the
818    end of file has been reached.
819
820    read_rtx is not used in the compiler proper, but rather in
821    the utilities gen*.c that construct C code from machine descriptions.  */
822
823 bool
824 read_rtx (rtx *x, int *lineno)
825 {
826   static rtx queue_head, queue_next;
827   static int queue_lineno;
828   int c;
829
830   /* Do one-time initialization.  */
831   if (queue_head == 0)
832     {
833       init_md_reader ();
834       initialize_iterators ();
835       queue_head = rtx_alloc (EXPR_LIST);
836     }
837
838   if (queue_next == 0)
839     {
840       struct map_value *mode_maps;
841       struct iterator_traverse_data mtd;
842       rtx from_file;
843
844       c = read_skip_spaces ();
845       if (c == EOF)
846         return false;
847       unread_char (c);
848
849       queue_lineno = read_md_lineno;
850       mode_maps = 0;
851       from_file = read_rtx_1 (&mode_maps);
852       if (from_file == 0)
853         return false;  /* This confuses a top level (nil) with end of
854                           file, but a top level (nil) would have
855                           crashed our caller anyway.  */
856
857       queue_next = queue_head;
858       XEXP (queue_next, 0) = from_file;
859       XEXP (queue_next, 1) = 0;
860
861       mtd.queue = queue_next;
862       mtd.mode_maps = mode_maps;
863       mtd.unknown_mode_attr = mode_maps ? mode_maps->string : NULL;
864       htab_traverse (modes.iterators, apply_iterator_traverse, &mtd);
865       htab_traverse (codes.iterators, apply_iterator_traverse, &mtd);
866       if (mtd.unknown_mode_attr)
867         fatal_with_file_and_line ("undefined attribute '%s' used for mode",
868                                   mtd.unknown_mode_attr);
869     }
870
871   *x = XEXP (queue_next, 0);
872   *lineno = queue_lineno;
873   queue_next = XEXP (queue_next, 1);
874
875   return true;
876 }
877
878 /* Subroutine of read_rtx that reads one construct from the MD file but
879    doesn't apply any iterators.  */
880
881 static rtx
882 read_rtx_1 (struct map_value **mode_maps)
883 {
884   int i;
885   RTX_CODE real_code, bellwether_code;
886   const char *format_ptr;
887   struct md_name name;
888   rtx return_rtx;
889   int c;
890   int tmp_int;
891   HOST_WIDE_INT tmp_wide;
892
893   /* Linked list structure for making RTXs: */
894   struct rtx_list
895     {
896       struct rtx_list *next;
897       rtx value;                /* Value of this node.  */
898     };
899
900  again:
901   c = read_skip_spaces (); /* Should be open paren.  */
902
903   if (c == EOF)
904     return 0;
905
906   if (c != '(')
907     fatal_expected_char ('(', c);
908
909   read_name (&name);
910   if (strcmp (name.string, "nil") == 0)
911     {
912       /* (nil) stands for an expression that isn't there.  */
913       c = read_skip_spaces ();
914       if (c != ')')
915         fatal_expected_char (')', c);
916       return 0;
917     }
918   if (strcmp (name.string, "define_constants") == 0)
919     {
920       read_constants ();
921       goto again;
922     }
923   if (strcmp (name.string, "define_conditions") == 0)
924     {
925       read_conditions ();
926       goto again;
927     }
928   if (strcmp (name.string, "define_mode_attr") == 0)
929     {
930       read_mapping (&modes, modes.attrs);
931       goto again;
932     }
933   if (strcmp (name.string, "define_mode_iterator") == 0)
934     {
935       read_mapping (&modes, modes.iterators);
936       goto again;
937     }
938   if (strcmp (name.string, "define_code_attr") == 0)
939     {
940       read_mapping (&codes, codes.attrs);
941       goto again;
942     }
943   if (strcmp (name.string, "define_code_iterator") == 0)
944     {
945       check_code_iterator (read_mapping (&codes, codes.iterators));
946       goto again;
947     }
948   real_code = (enum rtx_code) find_iterator (&codes, name.string);
949   bellwether_code = BELLWETHER_CODE (real_code);
950
951   /* If we end up with an insn expression then we free this space below.  */
952   return_rtx = rtx_alloc (bellwether_code);
953   format_ptr = GET_RTX_FORMAT (bellwether_code);
954   PUT_CODE (return_rtx, real_code);
955
956   /* If what follows is `: mode ', read it and
957      store the mode in the rtx.  */
958
959   i = read_skip_spaces ();
960   if (i == ':')
961     {
962       unsigned int mode;
963
964       read_name (&name);
965       if (name.string[0] != '<' || name.string[strlen (name.string) - 1] != '>')
966         mode = find_iterator (&modes, name.string);
967       else
968         mode = mode_attr_index (mode_maps, name.string);
969       PUT_MODE (return_rtx, (enum machine_mode) mode);
970       if (GET_MODE (return_rtx) != mode)
971         fatal_with_file_and_line ("mode too large");
972     }
973   else
974     unread_char (i);
975
976   for (i = 0; format_ptr[i] != 0; i++)
977     switch (format_ptr[i])
978       {
979         /* 0 means a field for internal use only.
980            Don't expect it to be present in the input.  */
981       case '0':
982         break;
983
984       case 'e':
985       case 'u':
986         XEXP (return_rtx, i) = read_rtx_1 (mode_maps);
987         break;
988
989       case 'V':
990         /* 'V' is an optional vector: if a closeparen follows,
991            just store NULL for this element.  */
992         c = read_skip_spaces ();
993         unread_char (c);
994         if (c == ')')
995           {
996             XVEC (return_rtx, i) = 0;
997             break;
998           }
999         /* Now process the vector.  */
1000
1001       case 'E':
1002         {
1003           /* Obstack to store scratch vector in.  */
1004           struct obstack vector_stack;
1005           int list_counter = 0;
1006           rtvec return_vec = NULL_RTVEC;
1007
1008           c = read_skip_spaces ();
1009           if (c != '[')
1010             fatal_expected_char ('[', c);
1011
1012           /* Add expressions to a list, while keeping a count.  */
1013           obstack_init (&vector_stack);
1014           while ((c = read_skip_spaces ()) && c != ']')
1015             {
1016               if (c == EOF)
1017                 fatal_expected_char (']', c);
1018               unread_char (c);
1019               list_counter++;
1020               obstack_ptr_grow (&vector_stack, read_rtx_1 (mode_maps));
1021             }
1022           if (list_counter > 0)
1023             {
1024               return_vec = rtvec_alloc (list_counter);
1025               memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
1026                       list_counter * sizeof (rtx));
1027             }
1028           else if (format_ptr[i] == 'E')
1029             fatal_with_file_and_line ("vector must have at least one element");
1030           XVEC (return_rtx, i) = return_vec;
1031           obstack_free (&vector_stack, NULL);
1032           /* close bracket gotten */
1033         }
1034         break;
1035
1036       case 'S':
1037       case 'T':
1038       case 's':
1039         {
1040           char *stringbuf;
1041           int star_if_braced;
1042
1043           c = read_skip_spaces ();
1044           unread_char (c);
1045           if (c == ')')
1046             {
1047               /* 'S' fields are optional and should be NULL if no string
1048                  was given.  Also allow normal 's' and 'T' strings to be
1049                  omitted, treating them in the same way as empty strings.  */
1050               XSTR (return_rtx, i) = (format_ptr[i] == 'S' ? NULL : "");
1051               break;
1052             }
1053
1054           /* The output template slot of a DEFINE_INSN,
1055              DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
1056              gets a star inserted as its first character, if it is
1057              written with a brace block instead of a string constant.  */
1058           star_if_braced = (format_ptr[i] == 'T');
1059
1060           stringbuf = read_string (star_if_braced);
1061
1062           /* For insn patterns, we want to provide a default name
1063              based on the file and line, like "*foo.md:12", if the
1064              given name is blank.  These are only for define_insn and
1065              define_insn_and_split, to aid debugging.  */
1066           if (*stringbuf == '\0'
1067               && i == 0
1068               && (GET_CODE (return_rtx) == DEFINE_INSN
1069                   || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT))
1070             {
1071               char line_name[20];
1072               const char *fn = (read_md_filename ? read_md_filename : "rtx");
1073               const char *slash;
1074               for (slash = fn; *slash; slash ++)
1075                 if (*slash == '/' || *slash == '\\' || *slash == ':')
1076                   fn = slash + 1;
1077               obstack_1grow (&string_obstack, '*');
1078               obstack_grow (&string_obstack, fn, strlen (fn));
1079               sprintf (line_name, ":%d", read_md_lineno);
1080               obstack_grow (&string_obstack, line_name, strlen (line_name)+1);
1081               stringbuf = XOBFINISH (&string_obstack, char *);
1082             }
1083
1084           if (star_if_braced)
1085             XTMPL (return_rtx, i) = stringbuf;
1086           else
1087             XSTR (return_rtx, i) = stringbuf;
1088         }
1089         break;
1090
1091       case 'w':
1092         read_name (&name);
1093         validate_const_int (name.string);
1094 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1095         tmp_wide = atoi (name.string);
1096 #else
1097 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1098         tmp_wide = atol (name.string);
1099 #else
1100         /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1101            But prefer not to use our hand-rolled function above either.  */
1102 #if defined(HAVE_ATOLL) || !defined(HAVE_ATOQ)
1103         tmp_wide = atoll (name.string);
1104 #else
1105         tmp_wide = atoq (name.string);
1106 #endif
1107 #endif
1108 #endif
1109         XWINT (return_rtx, i) = tmp_wide;
1110         break;
1111
1112       case 'i':
1113       case 'n':
1114         read_name (&name);
1115         validate_const_int (name.string);
1116         tmp_int = atoi (name.string);
1117         XINT (return_rtx, i) = tmp_int;
1118         break;
1119
1120       default:
1121         gcc_unreachable ();
1122       }
1123
1124   c = read_skip_spaces ();
1125   if (c != ')')
1126     {
1127       /* Syntactic sugar for AND and IOR, allowing Lisp-like
1128          arbitrary number of arguments for them.  */
1129       if (c == '(' && (GET_CODE (return_rtx) == AND
1130                        || GET_CODE (return_rtx) == IOR))
1131         return read_rtx_variadic (mode_maps, return_rtx);
1132       else
1133         fatal_expected_char (')', c);
1134     }
1135
1136   return return_rtx;
1137 }
1138
1139 /* Mutually recursive subroutine of read_rtx which reads
1140    (thing x1 x2 x3 ...) and produces RTL as if
1141    (thing x1 (thing x2 (thing x3 ...)))  had been written.
1142    When called, FORM is (thing x1 x2), and the file position
1143    is just past the leading parenthesis of x3.  Only works
1144    for THINGs which are dyadic expressions, e.g. AND, IOR.  */
1145 static rtx
1146 read_rtx_variadic (struct map_value **mode_maps, rtx form)
1147 {
1148   char c = '(';
1149   rtx p = form, q;
1150
1151   do
1152     {
1153       unread_char (c);
1154
1155       q = rtx_alloc (GET_CODE (p));
1156       PUT_MODE (q, GET_MODE (p));
1157
1158       XEXP (q, 0) = XEXP (p, 1);
1159       XEXP (q, 1) = read_rtx_1 (mode_maps);
1160
1161       XEXP (p, 1) = q;
1162       p = q;
1163       c = read_skip_spaces ();
1164     }
1165   while (c == '(');
1166
1167   if (c != ')')
1168     fatal_expected_char (')', c);
1169
1170   return form;
1171 }