OSDN Git Service

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