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