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