OSDN Git Service

PR testsuite/33082
[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
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 macro 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 "gensupport.h"
34
35 static htab_t md_constants;
36
37 /* One element in a singly-linked list of (integer, string) pairs.  */
38 struct map_value {
39   struct map_value *next;
40   int number;
41   const char *string;
42 };
43
44 /* Maps a macro or attribute name to a list of (integer, string) pairs.
45    The integers are mode or code values; the strings are either C conditions
46    or attribute values.  */
47 struct mapping {
48   /* The name of the macro or attribute.  */
49   const char *name;
50
51   /* The group (modes or codes) to which the macro or attribute belongs.  */
52   struct macro_group *group;
53
54   /* Gives a unique number to the attribute or macro.  Numbers are
55      allocated consecutively, starting at 0.  */
56   int index;
57
58   /* The list of (integer, string) pairs.  */
59   struct map_value *values;
60 };
61
62 /* A structure for abstracting the common parts of code and mode macros.  */
63 struct macro_group {
64   /* Tables of "mapping" structures, one for attributes and one for macros.  */
65   htab_t attrs, macros;
66
67   /* The number of "real" modes or codes (and by extension, the first
68      number available for use as a macro placeholder).  */
69   int num_builtins;
70
71   /* Treat the given string as the name of a standard mode or code and
72      return its integer value.  Use the given file for error reporting.  */
73   int (*find_builtin) (const char *, FILE *);
74
75   /* Return true if the given rtx uses the given mode or code.  */
76   bool (*uses_macro_p) (rtx, int);
77
78   /* Make the given rtx use the given mode or code.  */
79   void (*apply_macro) (rtx, int);
80 };
81
82 /* Associates PTR (which can be a string, etc.) with the file location
83    specified by FILENAME and LINENO.  */
84 struct ptr_loc {
85   const void *ptr;
86   const char *filename;
87   int lineno;
88 };
89
90 /* A structure used to pass data from read_rtx to apply_macro_traverse
91    via htab_traverse.  */
92 struct macro_traverse_data {
93   /* Instruction queue.  */
94   rtx queue;
95   /* Attributes seen for modes.  */
96   struct map_value *mode_maps;
97   /* Input file.  */
98   FILE *infile;
99   /* The last unknown attribute used as a mode.  */
100   const char *unknown_mode_attr;
101 };
102
103 /* If CODE is the number of a code macro, return a real rtx code that
104    has the same format.  Return CODE otherwise.  */
105 #define BELLWETHER_CODE(CODE) \
106   ((CODE) < NUM_RTX_CODE ? CODE : bellwether_codes[CODE - NUM_RTX_CODE])
107
108 static void fatal_with_file_and_line (FILE *, const char *, ...)
109   ATTRIBUTE_PRINTF_2 ATTRIBUTE_NORETURN;
110 static void fatal_expected_char (FILE *, int, int) ATTRIBUTE_NORETURN;
111 static int find_mode (const char *, FILE *);
112 static bool uses_mode_macro_p (rtx, int);
113 static void apply_mode_macro (rtx, int);
114 static int find_code (const char *, FILE *);
115 static bool uses_code_macro_p (rtx, int);
116 static void apply_code_macro (rtx, int);
117 static const char *apply_macro_to_string (const char *, struct mapping *, int);
118 static rtx apply_macro_to_rtx (rtx, struct mapping *, int,
119                                struct map_value *, FILE *, const char **);
120 static bool uses_macro_p (rtx, struct mapping *);
121 static const char *add_condition_to_string (const char *, const char *);
122 static void add_condition_to_rtx (rtx, const char *);
123 static int apply_macro_traverse (void **, void *);
124 static struct mapping *add_mapping (struct macro_group *, htab_t t,
125                                     const char *, FILE *);
126 static struct map_value **add_map_value (struct map_value **,
127                                          int, const char *);
128 static void initialize_macros (void);
129 static void read_name (char *, FILE *);
130 static hashval_t leading_ptr_hash (const void *);
131 static int leading_ptr_eq_p (const void *, const void *);
132 static void set_rtx_ptr_loc (const void *, const char *, int);
133 static const struct ptr_loc *get_rtx_ptr_loc (const void *);
134 static char *read_string (FILE *, int);
135 static char *read_quoted_string (FILE *);
136 static char *read_braced_string (FILE *);
137 static void read_escape (FILE *);
138 static hashval_t def_hash (const void *);
139 static int def_name_eq_p (const void *, const void *);
140 static void read_constants (FILE *infile, char *tmp_char);
141 static void read_conditions (FILE *infile, char *tmp_char);
142 static void validate_const_int (FILE *, const char *);
143 static int find_macro (struct macro_group *, const char *, FILE *);
144 static struct mapping *read_mapping (struct macro_group *, htab_t, FILE *);
145 static void check_code_macro (struct mapping *, FILE *);
146 static rtx read_rtx_1 (FILE *, struct map_value **);
147 static rtx read_rtx_variadic (FILE *, struct map_value **, rtx);
148
149 /* The mode and code macro structures.  */
150 static struct macro_group modes, codes;
151
152 /* Index I is the value of BELLWETHER_CODE (I + NUM_RTX_CODE).  */
153 static enum rtx_code *bellwether_codes;
154
155 /* Obstack used for allocating RTL strings.  */
156 static struct obstack string_obstack;
157
158 /* A table of ptr_locs, hashed on the PTR field.  */
159 static htab_t ptr_locs;
160
161 /* An obstack for the above.  Plain xmalloc is a bit heavyweight for a
162    small structure like ptr_loc.  */
163 static struct obstack ptr_loc_obstack;
164
165 /* A hash table of triples (A, B, C), where each of A, B and C is a condition
166    and A is equivalent to "B && C".  This is used to keep track of the source
167    of conditions that are made up of separate rtx strings (such as the split
168    condition of a define_insn_and_split).  */
169 static htab_t joined_conditions;
170
171 /* An obstack for allocating joined_conditions entries.  */
172 static struct obstack joined_conditions_obstack;
173
174 /* Subroutines of read_rtx.  */
175
176 /* The current line number for the file.  */
177 int read_rtx_lineno = 1;
178
179 /* The filename for error reporting.  */
180 const char *read_rtx_filename = "<unknown>";
181
182 static void
183 fatal_with_file_and_line (FILE *infile, const char *msg, ...)
184 {
185   char context[64];
186   size_t i;
187   int c;
188   va_list ap;
189
190   va_start (ap, msg);
191
192   fprintf (stderr, "%s:%d: ", read_rtx_filename, read_rtx_lineno);
193   vfprintf (stderr, msg, ap);
194   putc ('\n', stderr);
195
196   /* Gather some following context.  */
197   for (i = 0; i < sizeof (context)-1; ++i)
198     {
199       c = getc (infile);
200       if (c == EOF)
201         break;
202       if (c == '\r' || c == '\n')
203         break;
204       context[i] = c;
205     }
206   context[i] = '\0';
207
208   fprintf (stderr, "%s:%d: following context is `%s'\n",
209            read_rtx_filename, read_rtx_lineno, context);
210
211   va_end (ap);
212   exit (1);
213 }
214
215 /* Dump code after printing a message.  Used when read_rtx finds
216    invalid data.  */
217
218 static void
219 fatal_expected_char (FILE *infile, int expected_c, int actual_c)
220 {
221   fatal_with_file_and_line (infile, "expected character `%c', found `%c'",
222                             expected_c, actual_c);
223 }
224
225 /* Implementations of the macro_group callbacks for modes.  */
226
227 static int
228 find_mode (const char *name, FILE *infile)
229 {
230   int i;
231
232   for (i = 0; i < NUM_MACHINE_MODES; i++)
233     if (strcmp (GET_MODE_NAME (i), name) == 0)
234       return i;
235
236   fatal_with_file_and_line (infile, "unknown mode `%s'", name);
237 }
238
239 static bool
240 uses_mode_macro_p (rtx x, int mode)
241 {
242   return (int) GET_MODE (x) == mode;
243 }
244
245 static void
246 apply_mode_macro (rtx x, int mode)
247 {
248   PUT_MODE (x, (enum machine_mode) mode);
249 }
250
251 /* Implementations of the macro_group callbacks for codes.  */
252
253 static int
254 find_code (const char *name, FILE *infile)
255 {
256   int i;
257
258   for (i = 0; i < NUM_RTX_CODE; i++)
259     if (strcmp (GET_RTX_NAME (i), name) == 0)
260       return i;
261
262   fatal_with_file_and_line (infile, "unknown rtx code `%s'", name);
263 }
264
265 static bool
266 uses_code_macro_p (rtx x, int code)
267 {
268   return (int) GET_CODE (x) == code;
269 }
270
271 static void
272 apply_code_macro (rtx x, int code)
273 {
274   PUT_CODE (x, (enum rtx_code) code);
275 }
276
277 /* Map a code or mode attribute string P to the underlying string for
278    MACRO and VALUE.  */
279
280 static struct map_value *
281 map_attr_string (const char *p, struct mapping *macro, int value)
282 {
283   const char *attr;
284   struct mapping *m;
285   struct map_value *v;
286
287   /* If there's a "macro:" prefix, check whether the macro name matches.
288      Set ATTR to the start of the attribute name.  */
289   attr = strchr (p, ':');
290   if (attr == 0)
291     attr = p;
292   else
293     {
294       if (strncmp (p, macro->name, attr - p) != 0
295           || macro->name[attr - p] != 0)
296         return 0;
297       attr++;
298     }
299
300   /* Find the attribute specification.  */
301   m = (struct mapping *) htab_find (macro->group->attrs, &attr);
302   if (m == 0)
303     return 0;
304
305   /* Find the attribute value for VALUE.  */
306   for (v = m->values; v != 0; v = v->next)
307     if (v->number == value)
308       break;
309
310   return v;
311 }
312
313 /* Given an attribute string used as a machine mode, return an index
314    to store in the machine mode to be translated by
315    apply_macro_to_rtx.  */
316
317 static unsigned int
318 mode_attr_index (struct map_value **mode_maps, const char *string)
319 {
320   char *p;
321   struct map_value *mv;
322
323   /* Copy the attribute string into permanent storage, without the
324      angle brackets around it.  */
325   obstack_grow0 (&string_obstack, string + 1, strlen (string) - 2);
326   p = XOBFINISH (&string_obstack, char *);
327
328   mv = XNEW (struct map_value);
329   mv->number = *mode_maps == 0 ? 0 : (*mode_maps)->number + 1;
330   mv->string = p;
331   mv->next = *mode_maps;
332   *mode_maps = mv;
333
334   /* We return a code which we can map back into this string: the
335      number of machine modes + the number of mode macros + the index
336      we just used.  */
337   return MAX_MACHINE_MODE + htab_elements (modes.macros) + mv->number;
338 }
339
340 /* Apply MODE_MAPS to the top level of X, expanding cases where an
341    attribute is used for a mode.  MACRO is the current macro we are
342    expanding, and VALUE is the value to which we are expanding it.
343    INFILE is used for error messages.  This sets *UNKNOWN to true if
344    we find a mode attribute which has not yet been defined, and does
345    not change it otherwise.  */
346
347 static void
348 apply_mode_maps (rtx x, struct map_value *mode_maps, struct mapping *macro,
349                  int value, FILE *infile, const char **unknown)
350 {
351   unsigned int offset;
352   int indx;
353   struct map_value *pm;
354
355   offset = MAX_MACHINE_MODE + htab_elements (modes.macros);
356   if (GET_MODE (x) < offset)
357     return;
358
359   indx = GET_MODE (x) - offset;
360   for (pm = mode_maps; pm; pm = pm->next)
361     {
362       if (pm->number == indx)
363         {
364           struct map_value *v;
365
366           v = map_attr_string (pm->string, macro, value);
367           if (v)
368             PUT_MODE (x, (enum machine_mode) find_mode (v->string, infile));
369           else
370             *unknown = pm->string;
371           return;
372         }
373     }
374 }
375
376 /* Given that MACRO is being expanded as VALUE, apply the appropriate
377    string substitutions to STRING.  Return the new string if any changes
378    were needed, otherwise return STRING itself.  */
379
380 static const char *
381 apply_macro_to_string (const char *string, struct mapping *macro, int value)
382 {
383   char *base, *copy, *p, *start, *end;
384   struct map_value *v;
385
386   if (string == 0)
387     return string;
388
389   base = p = copy = ASTRDUP (string);
390   while ((start = strchr (p, '<')) && (end = strchr (start, '>')))
391     {
392       p = start + 1;
393
394       *end = 0;
395       v = map_attr_string (p, macro, value);
396       *end = '>';
397       if (v == 0)
398         continue;
399
400       /* Add everything between the last copied byte and the '<',
401          then add in the attribute value.  */
402       obstack_grow (&string_obstack, base, start - base);
403       obstack_grow (&string_obstack, v->string, strlen (v->string));
404       base = end + 1;
405     }
406   if (base != copy)
407     {
408       obstack_grow (&string_obstack, base, strlen (base) + 1);
409       copy = XOBFINISH (&string_obstack, char *);
410       copy_rtx_ptr_loc (copy, string);
411       return copy;
412     }
413   return string;
414 }
415
416 /* Return a copy of ORIGINAL in which all uses of MACRO have been
417    replaced by VALUE.  MODE_MAPS holds information about attribute
418    strings used for modes.  INFILE is used for error messages.  This
419    sets *UNKNOWN_MODE_ATTR to the value of an unknown mode attribute,
420    and does not change it otherwise.  */
421
422 static rtx
423 apply_macro_to_rtx (rtx original, struct mapping *macro, int value,
424                     struct map_value *mode_maps, FILE *infile,
425                     const char **unknown_mode_attr)
426 {
427   struct macro_group *group;
428   const char *format_ptr;
429   int i, j;
430   rtx x;
431   enum rtx_code bellwether_code;
432
433   if (original == 0)
434     return original;
435
436   /* Create a shallow copy of ORIGINAL.  */
437   bellwether_code = BELLWETHER_CODE (GET_CODE (original));
438   x = rtx_alloc (bellwether_code);
439   memcpy (x, original, RTX_CODE_SIZE (bellwether_code));
440
441   /* Change the mode or code itself.  */
442   group = macro->group;
443   if (group->uses_macro_p (x, macro->index + group->num_builtins))
444     group->apply_macro (x, value);
445
446   if (mode_maps)
447     apply_mode_maps (x, mode_maps, macro, value, infile, unknown_mode_attr);
448
449   /* Change each string and recursively change each rtx.  */
450   format_ptr = GET_RTX_FORMAT (bellwether_code);
451   for (i = 0; format_ptr[i] != 0; i++)
452     switch (format_ptr[i])
453       {
454       case 'T':
455         XTMPL (x, i) = apply_macro_to_string (XTMPL (x, i), macro, value);
456         break;
457
458       case 'S':
459       case 's':
460         XSTR (x, i) = apply_macro_to_string (XSTR (x, i), macro, value);
461         break;
462
463       case 'e':
464         XEXP (x, i) = apply_macro_to_rtx (XEXP (x, i), macro, value,
465                                           mode_maps, infile,
466                                           unknown_mode_attr);
467         break;
468
469       case 'V':
470       case 'E':
471         if (XVEC (original, i))
472           {
473             XVEC (x, i) = rtvec_alloc (XVECLEN (original, i));
474             for (j = 0; j < XVECLEN (x, i); j++)
475               XVECEXP (x, i, j) = apply_macro_to_rtx (XVECEXP (original, i, j),
476                                                       macro, value, mode_maps,
477                                                       infile,
478                                                       unknown_mode_attr);
479           }
480         break;
481
482       default:
483         break;
484       }
485   return x;
486 }
487
488 /* Return true if X (or some subexpression of X) uses macro MACRO.  */
489
490 static bool
491 uses_macro_p (rtx x, struct mapping *macro)
492 {
493   struct macro_group *group;
494   const char *format_ptr;
495   int i, j;
496
497   if (x == 0)
498     return false;
499
500   group = macro->group;
501   if (group->uses_macro_p (x, macro->index + group->num_builtins))
502     return true;
503
504   format_ptr = GET_RTX_FORMAT (BELLWETHER_CODE (GET_CODE (x)));
505   for (i = 0; format_ptr[i] != 0; i++)
506     switch (format_ptr[i])
507       {
508       case 'e':
509         if (uses_macro_p (XEXP (x, i), macro))
510           return true;
511         break;
512
513       case 'V':
514       case 'E':
515         if (XVEC (x, i))
516           for (j = 0; j < XVECLEN (x, i); j++)
517             if (uses_macro_p (XVECEXP (x, i, j), macro))
518               return true;
519         break;
520
521       default:
522         break;
523       }
524   return false;
525 }
526
527 /* Return a condition that must satisfy both ORIGINAL and EXTRA.  If ORIGINAL
528    has the form "&& ..." (as used in define_insn_and_splits), assume that
529    EXTRA is already satisfied.  Empty strings are treated like "true".  */
530
531 static const char *
532 add_condition_to_string (const char *original, const char *extra)
533 {
534   if (original != 0 && original[0] == '&' && original[1] == '&')
535     return original;
536   return join_c_conditions (original, extra);
537 }
538
539 /* Like add_condition, but applied to all conditions in rtx X.  */
540
541 static void
542 add_condition_to_rtx (rtx x, const char *extra)
543 {
544   switch (GET_CODE (x))
545     {
546     case DEFINE_INSN:
547     case DEFINE_EXPAND:
548       XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
549       break;
550
551     case DEFINE_SPLIT:
552     case DEFINE_PEEPHOLE:
553     case DEFINE_PEEPHOLE2:
554     case DEFINE_COND_EXEC:
555       XSTR (x, 1) = add_condition_to_string (XSTR (x, 1), extra);
556       break;
557
558     case DEFINE_INSN_AND_SPLIT:
559       XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
560       XSTR (x, 4) = add_condition_to_string (XSTR (x, 4), extra);
561       break;
562
563     default:
564       break;
565     }
566 }
567
568 /* A htab_traverse callback.  Search the EXPR_LIST given by DATA
569    for rtxes that use the macro in *SLOT.  Replace each such rtx
570    with a list of expansions.  */
571
572 static int
573 apply_macro_traverse (void **slot, void *data)
574 {
575   struct macro_traverse_data *mtd = (struct macro_traverse_data *) data;
576   struct mapping *macro;
577   struct map_value *v;
578   rtx elem, new_elem, original, x;
579
580   macro = (struct mapping *) *slot;
581   for (elem = mtd->queue; elem != 0; elem = XEXP (elem, 1))
582     if (uses_macro_p (XEXP (elem, 0), macro))
583       {
584         /* For each macro we expand, we set UNKNOWN_MODE_ATTR to NULL.
585            If apply_macro_rtx finds an unknown attribute for a mode,
586            it will set it to the attribute.  We want to know whether
587            the attribute is unknown after we have expanded all
588            possible macros, so setting it to NULL here gives us the
589            right result when the hash table traversal is complete.  */
590         mtd->unknown_mode_attr = NULL;
591
592         original = XEXP (elem, 0);
593         for (v = macro->values; v != 0; v = v->next)
594           {
595             x = apply_macro_to_rtx (original, macro, v->number,
596                                     mtd->mode_maps, mtd->infile,
597                                     &mtd->unknown_mode_attr);
598             add_condition_to_rtx (x, v->string);
599             if (v != macro->values)
600               {
601                 /* Insert a new EXPR_LIST node after ELEM and put the
602                    new expansion there.  */
603                 new_elem = rtx_alloc (EXPR_LIST);
604                 XEXP (new_elem, 1) = XEXP (elem, 1);
605                 XEXP (elem, 1) = new_elem;
606                 elem = new_elem;
607               }
608             XEXP (elem, 0) = x;
609           }
610     }
611   return 1;
612 }
613
614 /* Add a new "mapping" structure to hashtable TABLE.  NAME is the name
615    of the mapping, GROUP is the group to which it belongs, and INFILE
616    is the file that defined the mapping.  */
617
618 static struct mapping *
619 add_mapping (struct macro_group *group, htab_t table,
620              const char *name, FILE *infile)
621 {
622   struct mapping *m;
623   void **slot;
624
625   m = XNEW (struct mapping);
626   m->name = xstrdup (name);
627   m->group = group;
628   m->index = htab_elements (table);
629   m->values = 0;
630
631   slot = htab_find_slot (table, m, INSERT);
632   if (*slot != 0)
633     fatal_with_file_and_line (infile, "`%s' already defined", name);
634
635   *slot = m;
636   return m;
637 }
638
639 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
640    END_PTR points to the current null terminator for the list; return
641    a pointer the new null terminator.  */
642
643 static struct map_value **
644 add_map_value (struct map_value **end_ptr, int number, const char *string)
645 {
646   struct map_value *value;
647
648   value = XNEW (struct map_value);
649   value->next = 0;
650   value->number = number;
651   value->string = string;
652
653   *end_ptr = value;
654   return &value->next;
655 }
656
657 /* Do one-time initialization of the mode and code attributes.  */
658
659 static void
660 initialize_macros (void)
661 {
662   struct mapping *lower, *upper;
663   struct map_value **lower_ptr, **upper_ptr;
664   char *copy, *p;
665   int i;
666
667   modes.attrs = htab_create (13, def_hash, def_name_eq_p, 0);
668   modes.macros = htab_create (13, def_hash, def_name_eq_p, 0);
669   modes.num_builtins = MAX_MACHINE_MODE;
670   modes.find_builtin = find_mode;
671   modes.uses_macro_p = uses_mode_macro_p;
672   modes.apply_macro = apply_mode_macro;
673
674   codes.attrs = htab_create (13, def_hash, def_name_eq_p, 0);
675   codes.macros = htab_create (13, def_hash, def_name_eq_p, 0);
676   codes.num_builtins = NUM_RTX_CODE;
677   codes.find_builtin = find_code;
678   codes.uses_macro_p = uses_code_macro_p;
679   codes.apply_macro = apply_code_macro;
680
681   lower = add_mapping (&modes, modes.attrs, "mode", 0);
682   upper = add_mapping (&modes, modes.attrs, "MODE", 0);
683   lower_ptr = &lower->values;
684   upper_ptr = &upper->values;
685   for (i = 0; i < MAX_MACHINE_MODE; i++)
686     {
687       copy = xstrdup (GET_MODE_NAME (i));
688       for (p = copy; *p != 0; p++)
689         *p = TOLOWER (*p);
690
691       upper_ptr = add_map_value (upper_ptr, i, GET_MODE_NAME (i));
692       lower_ptr = add_map_value (lower_ptr, i, copy);
693     }
694
695   lower = add_mapping (&codes, codes.attrs, "code", 0);
696   upper = add_mapping (&codes, codes.attrs, "CODE", 0);
697   lower_ptr = &lower->values;
698   upper_ptr = &upper->values;
699   for (i = 0; i < NUM_RTX_CODE; i++)
700     {
701       copy = xstrdup (GET_RTX_NAME (i));
702       for (p = copy; *p != 0; p++)
703         *p = TOUPPER (*p);
704
705       lower_ptr = add_map_value (lower_ptr, i, GET_RTX_NAME (i));
706       upper_ptr = add_map_value (upper_ptr, i, copy);
707     }
708 }
709
710 /* Return a hash value for the pointer pointed to by DEF.  */
711
712 static hashval_t
713 leading_ptr_hash (const void *def)
714 {
715   return htab_hash_pointer (*(const void *const *) def);
716 }
717
718 /* Return true if DEF1 and DEF2 are pointers to the same pointer.  */
719
720 static int
721 leading_ptr_eq_p (const void *def1, const void *def2)
722 {
723   return *(const void *const *) def1 == *(const void *const *) def2;
724 }
725
726 /* Associate PTR with the file position given by FILENAME and LINENO.  */
727
728 static void
729 set_rtx_ptr_loc (const void *ptr, const char *filename, int lineno)
730 {
731   struct ptr_loc *loc;
732
733   loc = (struct ptr_loc *) obstack_alloc (&ptr_loc_obstack,
734                                           sizeof (struct ptr_loc));
735   loc->ptr = ptr;
736   loc->filename = filename;
737   loc->lineno = lineno;
738   *htab_find_slot (ptr_locs, loc, INSERT) = loc;
739 }
740
741 /* Return the position associated with pointer PTR.  Return null if no
742    position was set.  */
743
744 static const struct ptr_loc *
745 get_rtx_ptr_loc (const void *ptr)
746 {
747   return (const struct ptr_loc *) htab_find (ptr_locs, &ptr);
748 }
749
750 /* Associate NEW_PTR with the same file position as OLD_PTR.  */
751
752 void
753 copy_rtx_ptr_loc (const void *new_ptr, const void *old_ptr)
754 {
755   const struct ptr_loc *loc = get_rtx_ptr_loc (old_ptr);
756   if (loc != 0)
757     set_rtx_ptr_loc (new_ptr, loc->filename, loc->lineno);
758 }
759
760 /* If PTR is associated with a known file position, print a #line
761    directive for it.  */
762
763 void
764 print_rtx_ptr_loc (const void *ptr)
765 {
766   const struct ptr_loc *loc = get_rtx_ptr_loc (ptr);
767   if (loc != 0)
768     printf ("#line %d \"%s\"\n", loc->lineno, loc->filename);
769 }
770
771 /* Return a condition that satisfies both COND1 and COND2.  Either string
772    may be null or empty.  */
773
774 const char *
775 join_c_conditions (const char *cond1, const char *cond2)
776 {
777   char *result;
778   const void **entry;
779
780   if (cond1 == 0 || cond1[0] == 0)
781     return cond2;
782
783   if (cond2 == 0 || cond2[0] == 0)
784     return cond1;
785
786   result = concat ("(", cond1, ") && (", cond2, ")", NULL);
787   obstack_ptr_grow (&joined_conditions_obstack, result);
788   obstack_ptr_grow (&joined_conditions_obstack, cond1);
789   obstack_ptr_grow (&joined_conditions_obstack, cond2);
790   entry = XOBFINISH (&joined_conditions_obstack, const void **);
791   *htab_find_slot (joined_conditions, entry, INSERT) = entry;
792   return result;
793 }
794
795 /* Print condition COND, wrapped in brackets.  If COND was created by
796    join_c_conditions, recursively invoke this function for the original
797    conditions and join the result with "&&".  Otherwise print a #line
798    directive for COND if its original file position is known.  */
799
800 void
801 print_c_condition (const char *cond)
802 {
803   const char **halves = (const char **) htab_find (joined_conditions, &cond);
804   if (halves != 0)
805     {
806       printf ("(");
807       print_c_condition (halves[1]);
808       printf (" && ");
809       print_c_condition (halves[2]);
810       printf (")");
811     }
812   else
813     {
814       putc ('\n', stdout);
815       print_rtx_ptr_loc (cond);
816       printf ("(%s)", cond);
817     }
818 }
819
820 /* Read chars from INFILE until a non-whitespace char
821    and return that.  Comments, both Lisp style and C style,
822    are treated as whitespace.
823    Tools such as genflags use this function.  */
824
825 int
826 read_skip_spaces (FILE *infile)
827 {
828   int c;
829
830   while (1)
831     {
832       c = getc (infile);
833       switch (c)
834         {
835         case '\n':
836           read_rtx_lineno++;
837           break;
838
839         case ' ': case '\t': case '\f': case '\r':
840           break;
841
842         case ';':
843           do
844             c = getc (infile);
845           while (c != '\n' && c != EOF);
846           read_rtx_lineno++;
847           break;
848
849         case '/':
850           {
851             int prevc;
852             c = getc (infile);
853             if (c != '*')
854               fatal_expected_char (infile, '*', c);
855
856             prevc = 0;
857             while ((c = getc (infile)) && c != EOF)
858               {
859                 if (c == '\n')
860                    read_rtx_lineno++;
861                 else if (prevc == '*' && c == '/')
862                   break;
863                 prevc = c;
864               }
865           }
866           break;
867
868         default:
869           return c;
870         }
871     }
872 }
873
874 /* Read an rtx code name into the buffer STR[].
875    It is terminated by any of the punctuation chars of rtx printed syntax.  */
876
877 static void
878 read_name (char *str, FILE *infile)
879 {
880   char *p;
881   int c;
882
883   c = read_skip_spaces (infile);
884
885   p = str;
886   while (1)
887     {
888       if (c == ' ' || c == '\n' || c == '\t' || c == '\f' || c == '\r' || c == EOF)
889         break;
890       if (c == ':' || c == ')' || c == ']' || c == '"' || c == '/'
891           || c == '(' || c == '[')
892         {
893           ungetc (c, infile);
894           break;
895         }
896       *p++ = c;
897       c = getc (infile);
898     }
899   if (p == str)
900     fatal_with_file_and_line (infile, "missing name or number");
901   if (c == '\n')
902     read_rtx_lineno++;
903
904   *p = 0;
905
906   if (md_constants)
907     {
908       /* Do constant expansion.  */
909       struct md_constant *def;
910
911       p = str;
912       do
913         {
914           struct md_constant tmp_def;
915
916           tmp_def.name = p;
917           def = (struct md_constant *) htab_find (md_constants, &tmp_def);
918           if (def)
919             p = def->value;
920         } while (def);
921       if (p != str)
922         strcpy (str, p);
923     }
924 }
925
926 /* Subroutine of the string readers.  Handles backslash escapes.
927    Caller has read the backslash, but not placed it into the obstack.  */
928 static void
929 read_escape (FILE *infile)
930 {
931   int c = getc (infile);
932
933   switch (c)
934     {
935       /* Backslash-newline is replaced by nothing, as in C.  */
936     case '\n':
937       read_rtx_lineno++;
938       return;
939
940       /* \" \' \\ are replaced by the second character.  */
941     case '\\':
942     case '"':
943     case '\'':
944       break;
945
946       /* Standard C string escapes:
947          \a \b \f \n \r \t \v
948          \[0-7] \x
949          all are passed through to the output string unmolested.
950          In normal use these wind up in a string constant processed
951          by the C compiler, which will translate them appropriately.
952          We do not bother checking that \[0-7] are followed by up to
953          two octal digits, or that \x is followed by N hex digits.
954          \? \u \U are left out because they are not in traditional C.  */
955     case 'a': case 'b': case 'f': case 'n': case 'r': case 't': case 'v':
956     case '0': case '1': case '2': case '3': case '4': case '5': case '6':
957     case '7': case 'x':
958       obstack_1grow (&string_obstack, '\\');
959       break;
960
961       /* \; makes stuff for a C string constant containing
962          newline and tab.  */
963     case ';':
964       obstack_grow (&string_obstack, "\\n\\t", 4);
965       return;
966
967       /* pass anything else through, but issue a warning.  */
968     default:
969       fprintf (stderr, "%s:%d: warning: unrecognized escape \\%c\n",
970                read_rtx_filename, read_rtx_lineno, c);
971       obstack_1grow (&string_obstack, '\\');
972       break;
973     }
974
975   obstack_1grow (&string_obstack, c);
976 }
977
978
979 /* Read a double-quoted string onto the obstack.  Caller has scanned
980    the leading quote.  */
981 static char *
982 read_quoted_string (FILE *infile)
983 {
984   int c;
985
986   while (1)
987     {
988       c = getc (infile); /* Read the string  */
989       if (c == '\n')
990         read_rtx_lineno++;
991       else if (c == '\\')
992         {
993           read_escape (infile);
994           continue;
995         }
996       else if (c == '"' || c == EOF)
997         break;
998
999       obstack_1grow (&string_obstack, c);
1000     }
1001
1002   obstack_1grow (&string_obstack, 0);
1003   return XOBFINISH (&string_obstack, char *);
1004 }
1005
1006 /* Read a braced string (a la Tcl) onto the string obstack.  Caller
1007    has scanned the leading brace.  Note that unlike quoted strings,
1008    the outermost braces _are_ included in the string constant.  */
1009 static char *
1010 read_braced_string (FILE *infile)
1011 {
1012   int c;
1013   int brace_depth = 1;  /* caller-processed */
1014   unsigned long starting_read_rtx_lineno = read_rtx_lineno;
1015
1016   obstack_1grow (&string_obstack, '{');
1017   while (brace_depth)
1018     {
1019       c = getc (infile); /* Read the string  */
1020
1021       if (c == '\n')
1022         read_rtx_lineno++;
1023       else if (c == '{')
1024         brace_depth++;
1025       else if (c == '}')
1026         brace_depth--;
1027       else if (c == '\\')
1028         {
1029           read_escape (infile);
1030           continue;
1031         }
1032       else if (c == EOF)
1033         fatal_with_file_and_line
1034           (infile, "missing closing } for opening brace on line %lu",
1035            starting_read_rtx_lineno);
1036
1037       obstack_1grow (&string_obstack, c);
1038     }
1039
1040   obstack_1grow (&string_obstack, 0);
1041   return XOBFINISH (&string_obstack, char *);
1042 }
1043
1044 /* Read some kind of string constant.  This is the high-level routine
1045    used by read_rtx.  It handles surrounding parentheses, leading star,
1046    and dispatch to the appropriate string constant reader.  */
1047
1048 static char *
1049 read_string (FILE *infile, int star_if_braced)
1050 {
1051   char *stringbuf;
1052   int saw_paren = 0;
1053   int c, old_lineno;
1054
1055   c = read_skip_spaces (infile);
1056   if (c == '(')
1057     {
1058       saw_paren = 1;
1059       c = read_skip_spaces (infile);
1060     }
1061
1062   old_lineno = read_rtx_lineno;
1063   if (c == '"')
1064     stringbuf = read_quoted_string (infile);
1065   else if (c == '{')
1066     {
1067       if (star_if_braced)
1068         obstack_1grow (&string_obstack, '*');
1069       stringbuf = read_braced_string (infile);
1070     }
1071   else
1072     fatal_with_file_and_line (infile, "expected `\"' or `{', found `%c'", c);
1073
1074   if (saw_paren)
1075     {
1076       c = read_skip_spaces (infile);
1077       if (c != ')')
1078         fatal_expected_char (infile, ')', c);
1079     }
1080
1081   set_rtx_ptr_loc (stringbuf, read_rtx_filename, old_lineno);
1082   return stringbuf;
1083 }
1084 \f
1085 /* Provide a version of a function to read a long long if the system does
1086    not provide one.  */
1087 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !defined(HAVE_ATOLL) && !defined(HAVE_ATOQ)
1088 HOST_WIDE_INT atoll (const char *);
1089
1090 HOST_WIDE_INT
1091 atoll (const char *p)
1092 {
1093   int neg = 0;
1094   HOST_WIDE_INT tmp_wide;
1095
1096   while (ISSPACE (*p))
1097     p++;
1098   if (*p == '-')
1099     neg = 1, p++;
1100   else if (*p == '+')
1101     p++;
1102
1103   tmp_wide = 0;
1104   while (ISDIGIT (*p))
1105     {
1106       HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
1107       if (new_wide < tmp_wide)
1108         {
1109           /* Return INT_MAX equiv on overflow.  */
1110           tmp_wide = (~(unsigned HOST_WIDE_INT) 0) >> 1;
1111           break;
1112         }
1113       tmp_wide = new_wide;
1114       p++;
1115     }
1116
1117   if (neg)
1118     tmp_wide = -tmp_wide;
1119   return tmp_wide;
1120 }
1121 #endif
1122
1123 /* Given an object that starts with a char * name field, return a hash
1124    code for its name.  */
1125 static hashval_t
1126 def_hash (const void *def)
1127 {
1128   unsigned result, i;
1129   const char *string = *(const char *const *) def;
1130
1131   for (result = i = 0; *string++ != '\0'; i++)
1132     result += ((unsigned char) *string << (i % CHAR_BIT));
1133   return result;
1134 }
1135
1136 /* Given two objects that start with char * name fields, return true if
1137    they have the same name.  */
1138 static int
1139 def_name_eq_p (const void *def1, const void *def2)
1140 {
1141   return ! strcmp (*(const char *const *) def1,
1142                    *(const char *const *) def2);
1143 }
1144
1145 /* INFILE is a FILE pointer to read text from.  TMP_CHAR is a buffer suitable
1146    to read a name or number into.  Process a define_constants directive,
1147    starting with the optional space after the "define_constants".  */
1148 static void
1149 read_constants (FILE *infile, char *tmp_char)
1150 {
1151   int c;
1152   htab_t defs;
1153
1154   c = read_skip_spaces (infile);
1155   if (c != '[')
1156     fatal_expected_char (infile, '[', c);
1157   defs = md_constants;
1158   if (! defs)
1159     defs = htab_create (32, def_hash, def_name_eq_p, (htab_del) 0);
1160   /* Disable constant expansion during definition processing.  */
1161   md_constants = 0;
1162   while ( (c = read_skip_spaces (infile)) != ']')
1163     {
1164       struct md_constant *def;
1165       void **entry_ptr;
1166
1167       if (c != '(')
1168         fatal_expected_char (infile, '(', c);
1169       def = XNEW (struct md_constant);
1170       def->name = tmp_char;
1171       read_name (tmp_char, infile);
1172       entry_ptr = htab_find_slot (defs, def, INSERT);
1173       if (! *entry_ptr)
1174         def->name = xstrdup (tmp_char);
1175       c = read_skip_spaces (infile);
1176       ungetc (c, infile);
1177       read_name (tmp_char, infile);
1178       if (! *entry_ptr)
1179         {
1180           def->value = xstrdup (tmp_char);
1181           *entry_ptr = def;
1182         }
1183       else
1184         {
1185           def = (struct md_constant *) *entry_ptr;
1186           if (strcmp (def->value, tmp_char))
1187             fatal_with_file_and_line (infile,
1188                                       "redefinition of %s, was %s, now %s",
1189                                       def->name, def->value, tmp_char);
1190         }
1191       c = read_skip_spaces (infile);
1192       if (c != ')')
1193         fatal_expected_char (infile, ')', c);
1194     }
1195   md_constants = defs;
1196   c = read_skip_spaces (infile);
1197   if (c != ')')
1198     fatal_expected_char (infile, ')', c);
1199 }
1200
1201 /* For every constant definition, call CALLBACK with two arguments:
1202    a pointer a pointer to the constant definition and INFO.
1203    Stops when CALLBACK returns zero.  */
1204 void
1205 traverse_md_constants (htab_trav callback, void *info)
1206 {
1207   if (md_constants)
1208     htab_traverse (md_constants, callback, info);
1209 }
1210 \f
1211 /* INFILE is a FILE pointer to read text from.  TMP_CHAR is a buffer
1212    suitable to read a name or number into.  Process a
1213    define_conditions directive, starting with the optional space after
1214    the "define_conditions".  The directive looks like this:
1215
1216      (define_conditions [
1217         (number "string")
1218         (number "string")
1219         ...
1220      ])
1221
1222    It's not intended to appear in machine descriptions.  It is
1223    generated by (the program generated by) genconditions.c, and
1224    slipped in at the beginning of the sequence of MD files read by
1225    most of the other generators.  */
1226 static void
1227 read_conditions (FILE *infile, char *tmp_char)
1228 {
1229   int c;
1230
1231   c = read_skip_spaces (infile);
1232   if (c != '[')
1233     fatal_expected_char (infile, '[', c);
1234
1235   while ( (c = read_skip_spaces (infile)) != ']')
1236     {
1237       char *expr;
1238       int value;
1239
1240       if (c != '(')
1241         fatal_expected_char (infile, '(', c);
1242
1243       read_name (tmp_char, infile);
1244       validate_const_int (infile, tmp_char);
1245       value = atoi (tmp_char);
1246
1247       c = read_skip_spaces (infile);
1248       if (c != '"')
1249         fatal_expected_char (infile, '"', c);
1250       expr = read_quoted_string (infile);
1251
1252       c = read_skip_spaces (infile);
1253       if (c != ')')
1254         fatal_expected_char (infile, ')', c);
1255
1256       add_c_test (expr, value);
1257     }
1258   c = read_skip_spaces (infile);
1259   if (c != ')')
1260     fatal_expected_char (infile, ')', c);
1261 }
1262
1263 static void
1264 validate_const_int (FILE *infile, const char *string)
1265 {
1266   const char *cp;
1267   int valid = 1;
1268
1269   cp = string;
1270   while (*cp && ISSPACE (*cp))
1271     cp++;
1272   if (*cp == '-' || *cp == '+')
1273     cp++;
1274   if (*cp == 0)
1275     valid = 0;
1276   for (; *cp; cp++)
1277     if (! ISDIGIT (*cp))
1278       valid = 0;
1279   if (!valid)
1280     fatal_with_file_and_line (infile, "invalid decimal constant \"%s\"\n", string);
1281 }
1282
1283 /* Search GROUP for a mode or code called NAME and return its numerical
1284    identifier.  INFILE is the file that contained NAME.  */
1285
1286 static int
1287 find_macro (struct macro_group *group, const char *name, FILE *infile)
1288 {
1289   struct mapping *m;
1290
1291   m = (struct mapping *) htab_find (group->macros, &name);
1292   if (m != 0)
1293     return m->index + group->num_builtins;
1294   return group->find_builtin (name, infile);
1295 }
1296
1297 /* Finish reading a declaration of the form:
1298
1299        (define... <name> [<value1> ... <valuen>])
1300
1301    from INFILE, where each <valuei> is either a bare symbol name or a
1302    "(<name> <string>)" pair.  The "(define..." part has already been read.
1303
1304    Represent the declaration as a "mapping" structure; add it to TABLE
1305    (which belongs to GROUP) and return it.  */
1306
1307 static struct mapping *
1308 read_mapping (struct macro_group *group, htab_t table, FILE *infile)
1309 {
1310   char tmp_char[256];
1311   struct mapping *m;
1312   struct map_value **end_ptr;
1313   const char *string;
1314   int number, c;
1315
1316   /* Read the mapping name and create a structure for it.  */
1317   read_name (tmp_char, infile);
1318   m = add_mapping (group, table, tmp_char, infile);
1319
1320   c = read_skip_spaces (infile);
1321   if (c != '[')
1322     fatal_expected_char (infile, '[', c);
1323
1324   /* Read each value.  */
1325   end_ptr = &m->values;
1326   c = read_skip_spaces (infile);
1327   do
1328     {
1329       if (c != '(')
1330         {
1331           /* A bare symbol name that is implicitly paired to an
1332              empty string.  */
1333           ungetc (c, infile);
1334           read_name (tmp_char, infile);
1335           string = "";
1336         }
1337       else
1338         {
1339           /* A "(name string)" pair.  */
1340           read_name (tmp_char, infile);
1341           string = read_string (infile, false);
1342           c = read_skip_spaces (infile);
1343           if (c != ')')
1344             fatal_expected_char (infile, ')', c);
1345         }
1346       number = group->find_builtin (tmp_char, infile);
1347       end_ptr = add_map_value (end_ptr, number, string);
1348       c = read_skip_spaces (infile);
1349     }
1350   while (c != ']');
1351
1352   c = read_skip_spaces (infile);
1353   if (c != ')')
1354     fatal_expected_char (infile, ')', c);
1355
1356   return m;
1357 }
1358
1359 /* Check newly-created code macro MACRO to see whether every code has the
1360    same format.  Initialize the macro's entry in bellwether_codes.  */
1361
1362 static void
1363 check_code_macro (struct mapping *macro, FILE *infile)
1364 {
1365   struct map_value *v;
1366   enum rtx_code bellwether;
1367
1368   bellwether = (enum rtx_code) macro->values->number;
1369   for (v = macro->values->next; v != 0; v = v->next)
1370     if (strcmp (GET_RTX_FORMAT (bellwether), GET_RTX_FORMAT (v->number)) != 0)
1371       fatal_with_file_and_line (infile, "code macro `%s' combines "
1372                                 "different rtx formats", macro->name);
1373
1374   bellwether_codes = XRESIZEVEC (enum rtx_code, bellwether_codes,
1375                                  macro->index + 1);
1376   bellwether_codes[macro->index] = bellwether;
1377 }
1378
1379 /* Read an rtx in printed representation from INFILE and store its
1380    core representation in *X.  Also store the line number of the
1381    opening '(' in *LINENO.  Return true on success or false if the
1382    end of file has been reached.
1383
1384    read_rtx is not used in the compiler proper, but rather in
1385    the utilities gen*.c that construct C code from machine descriptions.  */
1386
1387 bool
1388 read_rtx (FILE *infile, rtx *x, int *lineno)
1389 {
1390   static rtx queue_head, queue_next;
1391   static int queue_lineno;
1392   int c;
1393
1394   /* Do one-time initialization.  */
1395   if (queue_head == 0)
1396     {
1397       initialize_macros ();
1398       obstack_init (&string_obstack);
1399       queue_head = rtx_alloc (EXPR_LIST);
1400       ptr_locs = htab_create (161, leading_ptr_hash, leading_ptr_eq_p, 0);
1401       obstack_init (&ptr_loc_obstack);
1402       joined_conditions = htab_create (161, leading_ptr_hash,
1403                                        leading_ptr_eq_p, 0);
1404       obstack_init (&joined_conditions_obstack);
1405     }
1406
1407   if (queue_next == 0)
1408     {
1409       struct map_value *mode_maps;
1410       struct macro_traverse_data mtd;
1411       rtx from_file;
1412
1413       c = read_skip_spaces (infile);
1414       if (c == EOF)
1415         return false;
1416       ungetc (c, infile);
1417
1418       queue_lineno = read_rtx_lineno;
1419       mode_maps = 0;
1420       from_file = read_rtx_1 (infile, &mode_maps);
1421       if (from_file == 0)
1422         return false;  /* This confuses a top level (nil) with end of
1423                           file, but a top level (nil) would have
1424                           crashed our caller anyway.  */
1425
1426       queue_next = queue_head;
1427       XEXP (queue_next, 0) = from_file;
1428       XEXP (queue_next, 1) = 0;
1429
1430       mtd.queue = queue_next;
1431       mtd.mode_maps = mode_maps;
1432       mtd.infile = infile;
1433       mtd.unknown_mode_attr = mode_maps ? mode_maps->string : NULL;
1434       htab_traverse (modes.macros, apply_macro_traverse, &mtd);
1435       htab_traverse (codes.macros, apply_macro_traverse, &mtd);
1436       if (mtd.unknown_mode_attr)
1437         fatal_with_file_and_line (infile,
1438                                   "undefined attribute '%s' used for mode",
1439                                   mtd.unknown_mode_attr);
1440     }
1441
1442   *x = XEXP (queue_next, 0);
1443   *lineno = queue_lineno;
1444   queue_next = XEXP (queue_next, 1);
1445
1446   return true;
1447 }
1448
1449 /* Subroutine of read_rtx that reads one construct from INFILE but
1450    doesn't apply any macros.  */
1451
1452 static rtx
1453 read_rtx_1 (FILE *infile, struct map_value **mode_maps)
1454 {
1455   int i;
1456   RTX_CODE real_code, bellwether_code;
1457   const char *format_ptr;
1458   /* tmp_char is a buffer used for reading decimal integers
1459      and names of rtx types and machine modes.
1460      Therefore, 256 must be enough.  */
1461   char tmp_char[256];
1462   rtx return_rtx;
1463   int c;
1464   int tmp_int;
1465   HOST_WIDE_INT tmp_wide;
1466
1467   /* Linked list structure for making RTXs: */
1468   struct rtx_list
1469     {
1470       struct rtx_list *next;
1471       rtx value;                /* Value of this node.  */
1472     };
1473
1474  again:
1475   c = read_skip_spaces (infile); /* Should be open paren.  */
1476
1477   if (c == EOF)
1478     return 0;
1479   
1480   if (c != '(')
1481     fatal_expected_char (infile, '(', c);
1482
1483   read_name (tmp_char, infile);
1484   if (strcmp (tmp_char, "nil") == 0)
1485     {
1486       /* (nil) stands for an expression that isn't there.  */
1487       c = read_skip_spaces (infile);
1488       if (c != ')')
1489         fatal_expected_char (infile, ')', c);
1490       return 0;
1491     }
1492   if (strcmp (tmp_char, "define_constants") == 0)
1493     {
1494       read_constants (infile, tmp_char);
1495       goto again;
1496     }
1497   if (strcmp (tmp_char, "define_conditions") == 0)
1498     {
1499       read_conditions (infile, tmp_char);
1500       goto again;
1501     }
1502   if (strcmp (tmp_char, "define_mode_attr") == 0)
1503     {
1504       read_mapping (&modes, modes.attrs, infile);
1505       goto again;
1506     }
1507   if (strcmp (tmp_char, "define_mode_macro") == 0)
1508     {
1509       read_mapping (&modes, modes.macros, infile);
1510       goto again;
1511     }
1512   if (strcmp (tmp_char, "define_code_attr") == 0)
1513     {
1514       read_mapping (&codes, codes.attrs, infile);
1515       goto again;
1516     }
1517   if (strcmp (tmp_char, "define_code_macro") == 0)
1518     {
1519       check_code_macro (read_mapping (&codes, codes.macros, infile), infile);
1520       goto again;
1521     }
1522   real_code = (enum rtx_code) find_macro (&codes, tmp_char, infile);
1523   bellwether_code = BELLWETHER_CODE (real_code);
1524
1525   /* If we end up with an insn expression then we free this space below.  */
1526   return_rtx = rtx_alloc (bellwether_code);
1527   format_ptr = GET_RTX_FORMAT (bellwether_code);
1528   PUT_CODE (return_rtx, real_code);
1529
1530   /* If what follows is `: mode ', read it and
1531      store the mode in the rtx.  */
1532
1533   i = read_skip_spaces (infile);
1534   if (i == ':')
1535     {
1536       unsigned int mode;
1537
1538       read_name (tmp_char, infile);
1539       if (tmp_char[0] != '<' || tmp_char[strlen (tmp_char) - 1] != '>')
1540         mode = find_macro (&modes, tmp_char, infile);
1541       else
1542         mode = mode_attr_index (mode_maps, tmp_char);
1543       PUT_MODE (return_rtx, (enum machine_mode) mode);
1544       if (GET_MODE (return_rtx) != mode)
1545         fatal_with_file_and_line (infile, "mode too large");
1546     }
1547   else
1548     ungetc (i, infile);
1549
1550   for (i = 0; format_ptr[i] != 0; i++)
1551     switch (format_ptr[i])
1552       {
1553         /* 0 means a field for internal use only.
1554            Don't expect it to be present in the input.  */
1555       case '0':
1556         break;
1557
1558       case 'e':
1559       case 'u':
1560         XEXP (return_rtx, i) = read_rtx_1 (infile, mode_maps);
1561         break;
1562
1563       case 'V':
1564         /* 'V' is an optional vector: if a closeparen follows,
1565            just store NULL for this element.  */
1566         c = read_skip_spaces (infile);
1567         ungetc (c, infile);
1568         if (c == ')')
1569           {
1570             XVEC (return_rtx, i) = 0;
1571             break;
1572           }
1573         /* Now process the vector.  */
1574
1575       case 'E':
1576         {
1577           /* Obstack to store scratch vector in.  */
1578           struct obstack vector_stack;
1579           int list_counter = 0;
1580           rtvec return_vec = NULL_RTVEC;
1581
1582           c = read_skip_spaces (infile);
1583           if (c != '[')
1584             fatal_expected_char (infile, '[', c);
1585
1586           /* Add expressions to a list, while keeping a count.  */
1587           obstack_init (&vector_stack);
1588           while ((c = read_skip_spaces (infile)) && c != ']')
1589             {
1590               ungetc (c, infile);
1591               list_counter++;
1592               obstack_ptr_grow (&vector_stack, read_rtx_1 (infile, mode_maps));
1593             }
1594           if (list_counter > 0)
1595             {
1596               return_vec = rtvec_alloc (list_counter);
1597               memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
1598                       list_counter * sizeof (rtx));
1599             }
1600           else if (format_ptr[i] == 'E')
1601             fatal_with_file_and_line (infile,
1602                                       "vector must have at least one element");
1603           XVEC (return_rtx, i) = return_vec;
1604           obstack_free (&vector_stack, NULL);
1605           /* close bracket gotten */
1606         }
1607         break;
1608
1609       case 'S':
1610       case 'T':
1611       case 's':
1612         {
1613           char *stringbuf;
1614           int star_if_braced;
1615
1616           c = read_skip_spaces (infile);
1617           ungetc (c, infile);
1618           if (c == ')')
1619             {
1620               /* 'S' fields are optional and should be NULL if no string
1621                  was given.  Also allow normal 's' and 'T' strings to be
1622                  omitted, treating them in the same way as empty strings.  */
1623               XSTR (return_rtx, i) = (format_ptr[i] == 'S' ? NULL : "");
1624               break;
1625             }
1626
1627           /* The output template slot of a DEFINE_INSN,
1628              DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
1629              gets a star inserted as its first character, if it is
1630              written with a brace block instead of a string constant.  */
1631           star_if_braced = (format_ptr[i] == 'T');
1632
1633           stringbuf = read_string (infile, star_if_braced);
1634
1635           /* For insn patterns, we want to provide a default name
1636              based on the file and line, like "*foo.md:12", if the
1637              given name is blank.  These are only for define_insn and
1638              define_insn_and_split, to aid debugging.  */
1639           if (*stringbuf == '\0'
1640               && i == 0
1641               && (GET_CODE (return_rtx) == DEFINE_INSN
1642                   || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT))
1643             {
1644               char line_name[20];
1645               const char *fn = (read_rtx_filename ? read_rtx_filename : "rtx");
1646               const char *slash;
1647               for (slash = fn; *slash; slash ++)
1648                 if (*slash == '/' || *slash == '\\' || *slash == ':')
1649                   fn = slash + 1;
1650               obstack_1grow (&string_obstack, '*');
1651               obstack_grow (&string_obstack, fn, strlen (fn));
1652               sprintf (line_name, ":%d", read_rtx_lineno);
1653               obstack_grow (&string_obstack, line_name, strlen (line_name)+1);
1654               stringbuf = XOBFINISH (&string_obstack, char *);
1655             }
1656
1657           if (star_if_braced)
1658             XTMPL (return_rtx, i) = stringbuf;
1659           else
1660             XSTR (return_rtx, i) = stringbuf;
1661         }
1662         break;
1663
1664       case 'w':
1665         read_name (tmp_char, infile);
1666         validate_const_int (infile, tmp_char);
1667 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1668         tmp_wide = atoi (tmp_char);
1669 #else
1670 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1671         tmp_wide = atol (tmp_char);
1672 #else
1673         /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1674            But prefer not to use our hand-rolled function above either.  */
1675 #if defined(HAVE_ATOLL) || !defined(HAVE_ATOQ)
1676         tmp_wide = atoll (tmp_char);
1677 #else
1678         tmp_wide = atoq (tmp_char);
1679 #endif
1680 #endif
1681 #endif
1682         XWINT (return_rtx, i) = tmp_wide;
1683         break;
1684
1685       case 'i':
1686       case 'n':
1687         read_name (tmp_char, infile);
1688         validate_const_int (infile, tmp_char);
1689         tmp_int = atoi (tmp_char);
1690         XINT (return_rtx, i) = tmp_int;
1691         break;
1692
1693       default:
1694         gcc_unreachable ();
1695       }
1696
1697   c = read_skip_spaces (infile);
1698   if (c != ')')
1699     {
1700       /* Syntactic sugar for AND and IOR, allowing Lisp-like
1701          arbitrary number of arguments for them.  */
1702       if (c == '(' && (GET_CODE (return_rtx) == AND
1703                        || GET_CODE (return_rtx) == IOR))
1704         return read_rtx_variadic (infile, mode_maps, return_rtx);
1705       else
1706         fatal_expected_char (infile, ')', c);
1707     }
1708
1709   return return_rtx;
1710 }
1711
1712 /* Mutually recursive subroutine of read_rtx which reads
1713    (thing x1 x2 x3 ...) and produces RTL as if
1714    (thing x1 (thing x2 (thing x3 ...)))  had been written.
1715    When called, FORM is (thing x1 x2), and the file position
1716    is just past the leading parenthesis of x3.  Only works
1717    for THINGs which are dyadic expressions, e.g. AND, IOR.  */
1718 static rtx
1719 read_rtx_variadic (FILE *infile, struct map_value **mode_maps, rtx form)
1720 {
1721   char c = '(';
1722   rtx p = form, q;
1723
1724   do
1725     {
1726       ungetc (c, infile);
1727
1728       q = rtx_alloc (GET_CODE (p));
1729       PUT_MODE (q, GET_MODE (p));
1730
1731       XEXP (q, 0) = XEXP (p, 1);
1732       XEXP (q, 1) = read_rtx_1 (infile, mode_maps);
1733       
1734       XEXP (p, 1) = q;
1735       p = q;
1736       c = read_skip_spaces (infile);
1737     }
1738   while (c == '(');
1739
1740   if (c != ')')
1741     fatal_expected_char (infile, ')', c);
1742
1743   return form;
1744 }