OSDN Git Service

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