OSDN Git Service

* rtl.h (REG_NON_LOCAL_GOTO): New.
[pf3gnuchains/gcc-fork.git] / gcc / rtl.c
1 /* Allocate and read RTL for GNU C Compiler.
2    Copyright (C) 1987, 1988, 1991, 1994, 1997, 1998, 1999, 2000
3    Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22
23 #include "config.h"
24 #include "system.h"
25 #include "rtl.h"
26 #include "real.h"
27 #include "bitmap.h"
28 #include "ggc.h"
29 #include "obstack.h"
30 #include "toplev.h"
31 #include "hashtab.h"
32
33 #define obstack_chunk_alloc     xmalloc
34 #define obstack_chunk_free      free
35
36 \f
37 /* Calculate the format for CONST_DOUBLE.  This depends on the relative
38    widths of HOST_WIDE_INT and REAL_VALUE_TYPE.
39
40    We need to go out to e0wwwww, since REAL_ARITHMETIC assumes 16-bits
41    per element in REAL_VALUE_TYPE.
42
43    This is duplicated in gengenrtl.c.
44
45    A number of places assume that there are always at least two 'w'
46    slots in a CONST_DOUBLE, so we provide them even if one would suffice.  */
47
48 #ifdef REAL_ARITHMETIC
49 # if MAX_LONG_DOUBLE_TYPE_SIZE == 96
50 #  define REAL_WIDTH    \
51      (11*8 + HOST_BITS_PER_WIDE_INT)/HOST_BITS_PER_WIDE_INT
52 # else
53 #  if MAX_LONG_DOUBLE_TYPE_SIZE == 128
54 #   define REAL_WIDTH   \
55       (19*8 + HOST_BITS_PER_WIDE_INT)/HOST_BITS_PER_WIDE_INT
56 #  else
57 #   if HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT
58 #    define REAL_WIDTH  \
59        (7*8 + HOST_BITS_PER_WIDE_INT)/HOST_BITS_PER_WIDE_INT
60 #   endif
61 #  endif
62 # endif
63 #endif /* REAL_ARITHMETIC */
64
65 #ifndef REAL_WIDTH
66 # if HOST_BITS_PER_WIDE_INT*2 >= MAX_LONG_DOUBLE_TYPE_SIZE
67 #  define REAL_WIDTH    2
68 # else
69 #  if HOST_BITS_PER_WIDE_INT*3 >= MAX_LONG_DOUBLE_TYPE_SIZE
70 #   define REAL_WIDTH   3
71 #  else
72 #   if HOST_BITS_PER_WIDE_INT*4 >= MAX_LONG_DOUBLE_TYPE_SIZE
73 #    define REAL_WIDTH  4
74 #   endif
75 #  endif
76 # endif
77 #endif /* REAL_WIDTH */
78
79 #if REAL_WIDTH == 1
80 # define CONST_DOUBLE_FORMAT    "e0ww"
81 #else
82 # if REAL_WIDTH == 2
83 #  define CONST_DOUBLE_FORMAT   "e0ww"
84 # else
85 #  if REAL_WIDTH == 3
86 #   define CONST_DOUBLE_FORMAT  "e0www"
87 #  else
88 #   if REAL_WIDTH == 4
89 #    define CONST_DOUBLE_FORMAT "e0wwww"
90 #   else
91 #    if REAL_WIDTH == 5
92 #     define CONST_DOUBLE_FORMAT        "e0wwwww"
93 #    else
94 #     define CONST_DOUBLE_FORMAT        /* nothing - will cause syntax error */
95 #    endif
96 #   endif
97 #  endif
98 # endif
99 #endif
100
101 /* Indexed by rtx code, gives number of operands for an rtx with that code.
102    Does NOT include rtx header data (code and links).  */
103
104 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS)   sizeof FORMAT - 1 ,
105
106 const int rtx_length[NUM_RTX_CODE + 1] = {
107 #include "rtl.def"
108 };
109
110 #undef DEF_RTL_EXPR
111
112 /* Indexed by rtx code, gives the name of that kind of rtx, as a C string.  */
113
114 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS)   NAME ,
115
116 const char * const rtx_name[] = {
117 #include "rtl.def"              /* rtl expressions are documented here */
118 };
119
120 #undef DEF_RTL_EXPR
121
122 /* Indexed by machine mode, gives the name of that machine mode.
123    This name does not include the letters "mode".  */
124
125 #define DEF_MACHMODE(SYM, NAME, CLASS, BITSIZE, SIZE, UNIT, WIDER)  NAME,
126
127 const char * const mode_name[(int) MAX_MACHINE_MODE + 1] = {
128 #include "machmode.def"
129   /* Add an extra field to avoid a core dump if someone tries to convert
130      MAX_MACHINE_MODE to a string.   */
131   ""
132 };
133
134 #undef DEF_MACHMODE
135
136 /* Indexed by machine mode, gives the class mode for GET_MODE_CLASS.  */
137
138 #define DEF_MACHMODE(SYM, NAME, CLASS, BITSIZE, SIZE, UNIT, WIDER)  CLASS,
139
140 const enum mode_class mode_class[(int) MAX_MACHINE_MODE] = {
141 #include "machmode.def"
142 };
143
144 #undef DEF_MACHMODE
145
146 /* Indexed by machine mode, gives the length of the mode, in bits.
147    GET_MODE_BITSIZE uses this.  */
148
149 #define DEF_MACHMODE(SYM, NAME, CLASS, BITSIZE, SIZE, UNIT, WIDER)  BITSIZE,
150
151 const unsigned int mode_bitsize[(int) MAX_MACHINE_MODE] = {
152 #include "machmode.def"
153 };
154
155 #undef DEF_MACHMODE
156
157 /* Indexed by machine mode, gives the length of the mode, in bytes.
158    GET_MODE_SIZE uses this.  */
159
160 #define DEF_MACHMODE(SYM, NAME, CLASS, BITSIZE, SIZE, UNIT, WIDER)  SIZE,
161
162 const unsigned int mode_size[(int) MAX_MACHINE_MODE] = {
163 #include "machmode.def"
164 };
165
166 #undef DEF_MACHMODE
167
168 /* Indexed by machine mode, gives the length of the mode's subunit.
169    GET_MODE_UNIT_SIZE uses this.  */
170
171 #define DEF_MACHMODE(SYM, NAME, CLASS, BITSIZE, SIZE, UNIT, WIDER)  UNIT,
172
173 const unsigned int mode_unit_size[(int) MAX_MACHINE_MODE] = {
174 #include "machmode.def"         /* machine modes are documented here */
175 };
176
177 #undef DEF_MACHMODE
178
179 /* Indexed by machine mode, gives next wider natural mode
180    (QI -> HI -> SI -> DI, etc.)  Widening multiply instructions
181    use this.  */
182
183 #define DEF_MACHMODE(SYM, NAME, CLASS, BITSIZE, SIZE, UNIT, WIDER)  \
184   (unsigned char) WIDER,
185
186 const unsigned char mode_wider_mode[(int) MAX_MACHINE_MODE] = {
187 #include "machmode.def"         /* machine modes are documented here */
188 };
189
190 #undef DEF_MACHMODE
191
192 #define DEF_MACHMODE(SYM, NAME, CLASS, BITSIZE, SIZE, UNIT, WIDER)  \
193   ((BITSIZE) >= HOST_BITS_PER_WIDE_INT) ? ~(unsigned HOST_WIDE_INT)0 : ((unsigned HOST_WIDE_INT) 1 << (BITSIZE)) - 1,
194
195 /* Indexed by machine mode, gives mask of significant bits in mode.  */
196
197 const unsigned HOST_WIDE_INT mode_mask_array[(int) MAX_MACHINE_MODE] = {
198 #include "machmode.def"
199 };
200
201 /* Indexed by mode class, gives the narrowest mode for each class.
202    The Q modes are always of width 1 (2 for complex) - it is impossible
203    for any mode to be narrower.
204
205    Note that we use QImode instead of BImode for MODE_INT, since
206    otherwise the middle end will try to use it for bitfields in
207    structures and the like, which we do not want.  Only the target
208    md file should generate BImode widgets.  */
209
210 const enum machine_mode class_narrowest_mode[(int) MAX_MODE_CLASS] = {
211     /* MODE_RANDOM */           VOIDmode,
212     /* MODE_INT */              QImode,
213     /* MODE_FLOAT */            QFmode,
214     /* MODE_PARTIAL_INT */      PQImode,
215     /* MODE_CC */               CCmode,
216     /* MODE_COMPLEX_INT */      CQImode,
217     /* MODE_COMPLEX_FLOAT */    QCmode,
218     /* MODE_VECTOR_INT */       V2QImode,
219     /* MODE_VECTOR_FLOAT */     V2SFmode
220 };
221
222
223 /* Indexed by rtx code, gives a sequence of operand-types for
224    rtx's of that code.  The sequence is a C string in which
225    each character describes one operand.  */
226
227 const char * const rtx_format[] = {
228   /* "*" undefined.
229          can cause a warning message
230      "0" field is unused (or used in a phase-dependent manner)
231          prints nothing
232      "i" an integer
233          prints the integer
234      "n" like "i", but prints entries from `note_insn_name'
235      "w" an integer of width HOST_BITS_PER_WIDE_INT
236          prints the integer
237      "s" a pointer to a string
238          prints the string
239      "S" like "s", but optional:
240          the containing rtx may end before this operand
241      "e" a pointer to an rtl expression
242          prints the expression
243      "E" a pointer to a vector that points to a number of rtl expressions
244          prints a list of the rtl expressions
245      "V" like "E", but optional:
246          the containing rtx may end before this operand
247      "u" a pointer to another insn
248          prints the uid of the insn.
249      "b" is a pointer to a bitmap header.
250      "t" is a tree pointer. */
251
252 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS)   FORMAT ,
253 #include "rtl.def"              /* rtl expressions are defined here */
254 #undef DEF_RTL_EXPR
255 };
256
257 /* Indexed by rtx code, gives a character representing the "class" of
258    that rtx code.  See rtl.def for documentation on the defined classes.  */
259
260 const char rtx_class[] = {
261 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS)   CLASS,
262 #include "rtl.def"              /* rtl expressions are defined here */
263 #undef DEF_RTL_EXPR
264 };
265
266 /* Names for kinds of NOTEs and REG_NOTEs.  */
267
268 const char * const note_insn_name[NOTE_INSN_MAX - NOTE_INSN_BIAS] =
269 {
270   "", "NOTE_INSN_DELETED",
271   "NOTE_INSN_BLOCK_BEG", "NOTE_INSN_BLOCK_END",
272   "NOTE_INSN_LOOP_BEG", "NOTE_INSN_LOOP_END",
273   "NOTE_INSN_LOOP_CONT", "NOTE_INSN_LOOP_VTOP",
274   "NOTE_INSN_FUNCTION_END", "NOTE_INSN_SETJMP",
275   "NOTE_INSN_PROLOGUE_END", "NOTE_INSN_EPILOGUE_BEG",
276   "NOTE_INSN_DELETED_LABEL", "NOTE_INSN_FUNCTION_BEG",
277   "NOTE_INSN_EH_REGION_BEG", "NOTE_INSN_EH_REGION_END",
278   "NOTE_INSN_REPEATED_LINE_NUMBER", "NOTE_INSN_RANGE_BEG",
279   "NOTE_INSN_RANGE_END", "NOTE_INSN_LIVE",
280   "NOTE_INSN_BASIC_BLOCK", "NOTE_INSN_EXPECTED_VALUE"
281 };
282
283 const char * const reg_note_name[] =
284 {
285   "", "REG_DEAD", "REG_INC", "REG_EQUIV", "REG_EQUAL",
286   "REG_WAS_0", "REG_RETVAL", "REG_LIBCALL", "REG_NONNEG",
287   "REG_NO_CONFLICT", "REG_UNUSED", "REG_CC_SETTER", "REG_CC_USER",
288   "REG_LABEL", "REG_DEP_ANTI", "REG_DEP_OUTPUT", "REG_BR_PROB",
289   "REG_EXEC_COUNT", "REG_NOALIAS", "REG_SAVE_AREA", "REG_BR_PRED",
290   "REG_FRAME_RELATED_EXPR", "REG_EH_CONTEXT", "REG_EH_REGION",
291   "REG_EH_RETHROW", "REG_SAVE_NOTE", "REG_MAYBE_DEAD", "REG_NORETURN",
292   "REG_NON_LOCAL_GOTO"
293 };
294
295 static htab_t md_constants;
296
297 static void fatal_with_file_and_line PARAMS ((FILE *, const char *, ...))
298   ATTRIBUTE_PRINTF_2 ATTRIBUTE_NORETURN;
299 static void fatal_expected_char PARAMS ((FILE *, int, int)) ATTRIBUTE_NORETURN;
300 static void read_name           PARAMS ((char *, FILE *));
301 static unsigned def_hash PARAMS ((const void *));
302 static int def_name_eq_p PARAMS ((const void *, const void *));
303 static void read_constants PARAMS ((FILE *infile, char *tmp_char));
304
305 \f
306 /* Allocate an rtx vector of N elements.
307    Store the length, and initialize all elements to zero.  */
308
309 rtvec
310 rtvec_alloc (n)
311      int n;
312 {
313   rtvec rt;
314
315   rt = ggc_alloc_rtvec (n);
316   /* clear out the vector */
317   memset (&rt->elem[0], 0, n * sizeof (rtx));
318
319   PUT_NUM_ELEM (rt, n);
320   return rt;
321 }
322
323 /* Allocate an rtx of code CODE.  The CODE is stored in the rtx;
324    all the rest is initialized to zero.  */
325
326 rtx
327 rtx_alloc (code)
328   RTX_CODE code;
329 {
330   rtx rt;
331   int n = GET_RTX_LENGTH (code);
332
333   rt = ggc_alloc_rtx (n);
334
335   /* We want to clear everything up to the FLD array.  Normally, this
336      is one int, but we don't want to assume that and it isn't very
337      portable anyway; this is.  */
338
339   memset (rt, 0, sizeof (struct rtx_def) - sizeof (rtunion));
340   PUT_CODE (rt, code);
341   return rt;
342 }
343
344 \f
345 /* Create a new copy of an rtx.
346    Recursively copies the operands of the rtx,
347    except for those few rtx codes that are sharable.  */
348
349 rtx
350 copy_rtx (orig)
351      register rtx orig;
352 {
353   register rtx copy;
354   register int i, j;
355   register RTX_CODE code;
356   register const char *format_ptr;
357
358   code = GET_CODE (orig);
359
360   switch (code)
361     {
362     case REG:
363     case QUEUED:
364     case CONST_INT:
365     case CONST_DOUBLE:
366     case SYMBOL_REF:
367     case CODE_LABEL:
368     case PC:
369     case CC0:
370     case SCRATCH:
371       /* SCRATCH must be shared because they represent distinct values.  */
372     case ADDRESSOF:
373       return orig;
374
375     case CONST:
376       /* CONST can be shared if it contains a SYMBOL_REF.  If it contains
377          a LABEL_REF, it isn't sharable.  */
378       if (GET_CODE (XEXP (orig, 0)) == PLUS
379           && GET_CODE (XEXP (XEXP (orig, 0), 0)) == SYMBOL_REF
380           && GET_CODE (XEXP (XEXP (orig, 0), 1)) == CONST_INT)
381         return orig;
382       break;
383
384       /* A MEM with a constant address is not sharable.  The problem is that
385          the constant address may need to be reloaded.  If the mem is shared,
386          then reloading one copy of this mem will cause all copies to appear
387          to have been reloaded.  */
388
389     default:
390       break;
391     }
392
393   copy = rtx_alloc (code);
394
395   /* Copy the various flags, and other information.  We assume that
396      all fields need copying, and then clear the fields that should
397      not be copied.  That is the sensible default behavior, and forces
398      us to explicitly document why we are *not* copying a flag.  */
399   memcpy (copy, orig, sizeof (struct rtx_def) - sizeof (rtunion));
400
401   /* We do not copy the USED flag, which is used as a mark bit during
402      walks over the RTL.  */
403   copy->used = 0;
404
405   /* We do not copy FRAME_RELATED for INSNs.  */
406   if (GET_RTX_CLASS (code) == 'i')
407     copy->frame_related = 0;
408   copy->jump = orig->jump;
409   copy->call = orig->call;
410
411   format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
412
413   for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
414     {
415       copy->fld[i] = orig->fld[i];
416       switch (*format_ptr++)
417         {
418         case 'e':
419           if (XEXP (orig, i) != NULL)
420             XEXP (copy, i) = copy_rtx (XEXP (orig, i));
421           break;
422
423         case 'E':
424         case 'V':
425           if (XVEC (orig, i) != NULL)
426             {
427               XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
428               for (j = 0; j < XVECLEN (copy, i); j++)
429                 XVECEXP (copy, i, j) = copy_rtx (XVECEXP (orig, i, j));
430             }
431           break;
432
433         case 't':
434         case 'w':
435         case 'i':
436         case 's':
437         case 'S':
438         case 'u':
439         case '0':
440           /* These are left unchanged.  */
441           break;
442
443         default:
444           abort ();
445         }
446     }
447   return copy;
448 }
449
450 /* Similar to `copy_rtx' except that if MAY_SHARE is present, it is
451    placed in the result directly, rather than being copied.  */
452
453 rtx
454 copy_most_rtx (orig, may_share)
455      register rtx orig;
456      register rtx may_share;
457 {
458   register rtx copy;
459   register int i, j;
460   register RTX_CODE code;
461   register const char *format_ptr;
462
463   if (orig == may_share)
464     return orig;
465
466   code = GET_CODE (orig);
467
468   switch (code)
469     {
470     case REG:
471     case QUEUED:
472     case CONST_INT:
473     case CONST_DOUBLE:
474     case SYMBOL_REF:
475     case CODE_LABEL:
476     case PC:
477     case CC0:
478       return orig;
479     default:
480       break;
481     }
482
483   copy = rtx_alloc (code);
484   PUT_MODE (copy, GET_MODE (orig));
485   copy->in_struct = orig->in_struct;
486   copy->volatil = orig->volatil;
487   copy->unchanging = orig->unchanging;
488   copy->integrated = orig->integrated;
489
490   format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
491
492   for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
493     {
494       switch (*format_ptr++)
495         {
496         case 'e':
497           XEXP (copy, i) = XEXP (orig, i);
498           if (XEXP (orig, i) != NULL && XEXP (orig, i) != may_share)
499             XEXP (copy, i) = copy_most_rtx (XEXP (orig, i), may_share);
500           break;
501
502         case 'u':
503           XEXP (copy, i) = XEXP (orig, i);
504           break;
505
506         case 'E':
507         case 'V':
508           XVEC (copy, i) = XVEC (orig, i);
509           if (XVEC (orig, i) != NULL)
510             {
511               XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
512               for (j = 0; j < XVECLEN (copy, i); j++)
513                 XVECEXP (copy, i, j)
514                   = copy_most_rtx (XVECEXP (orig, i, j), may_share);
515             }
516           break;
517
518         case 'w':
519           XWINT (copy, i) = XWINT (orig, i);
520           break;
521
522         case 'n':
523         case 'i':
524           XINT (copy, i) = XINT (orig, i);
525           break;
526
527         case 't':
528           XTREE (copy, i) = XTREE (orig, i);
529           break;
530
531         case 's':
532         case 'S':
533           XSTR (copy, i) = XSTR (orig, i);
534           break;
535
536         case '0':
537           /* Copy this through the wide int field; that's safest. */
538           X0WINT (copy, i) = X0WINT (orig, i);
539           break;
540
541         default:
542           abort ();
543         }
544     }
545   return copy;
546 }
547
548 /* Create a new copy of an rtx.  Only copy just one level.  */
549 rtx
550 shallow_copy_rtx (orig)
551      rtx orig;
552 {
553   register int i;
554   register RTX_CODE code = GET_CODE (orig);
555   register rtx copy = rtx_alloc (code);
556
557   PUT_MODE (copy, GET_MODE (orig));
558   copy->in_struct = orig->in_struct;
559   copy->volatil = orig->volatil;
560   copy->unchanging = orig->unchanging;
561   copy->integrated = orig->integrated;
562
563   for (i = 0; i < GET_RTX_LENGTH (code); i++)
564     copy->fld[i] = orig->fld[i];
565
566   return copy;
567 }
568 \f
569 /* This is 1 until after the rtl generation pass.  */
570 int rtx_equal_function_value_matters;
571
572 /* Nonzero when we are generating CONCATs.  */
573 int generating_concat_p;
574 \f
575 /* Return 1 if X and Y are identical-looking rtx's.
576    This is the Lisp function EQUAL for rtx arguments.  */
577
578 int
579 rtx_equal_p (x, y)
580      rtx x, y;
581 {
582   register int i;
583   register int j;
584   register enum rtx_code code;
585   register const char *fmt;
586
587   if (x == y)
588     return 1;
589   if (x == 0 || y == 0)
590     return 0;
591
592   code = GET_CODE (x);
593   /* Rtx's of different codes cannot be equal.  */
594   if (code != GET_CODE (y))
595     return 0;
596
597   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
598      (REG:SI x) and (REG:HI x) are NOT equivalent.  */
599
600   if (GET_MODE (x) != GET_MODE (y))
601     return 0;
602
603   /* Some RTL can be compared nonrecursively.  */
604   switch (code)
605     {
606     case REG:
607       /* Until rtl generation is complete, don't consider a reference to the
608          return register of the current function the same as the return from a
609          called function.  This eases the job of function integration.  Once the
610          distinction is no longer needed, they can be considered equivalent.  */
611       return (REGNO (x) == REGNO (y)
612               && (! rtx_equal_function_value_matters
613                   || REG_FUNCTION_VALUE_P (x) == REG_FUNCTION_VALUE_P (y)));
614
615     case LABEL_REF:
616       return XEXP (x, 0) == XEXP (y, 0);
617
618     case SYMBOL_REF:
619       return XSTR (x, 0) == XSTR (y, 0);
620
621     case SCRATCH:
622     case CONST_DOUBLE:
623     case CONST_INT:
624       return 0;
625
626     default:
627       break;
628     }
629
630   /* Compare the elements.  If any pair of corresponding elements
631      fail to match, return 0 for the whole things.  */
632
633   fmt = GET_RTX_FORMAT (code);
634   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
635     {
636       switch (fmt[i])
637         {
638         case 'w':
639           if (XWINT (x, i) != XWINT (y, i))
640             return 0;
641           break;
642
643         case 'n':
644         case 'i':
645           if (XINT (x, i) != XINT (y, i))
646             return 0;
647           break;
648
649         case 'V':
650         case 'E':
651           /* Two vectors must have the same length.  */
652           if (XVECLEN (x, i) != XVECLEN (y, i))
653             return 0;
654
655           /* And the corresponding elements must match.  */
656           for (j = 0; j < XVECLEN (x, i); j++)
657             if (rtx_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)) == 0)
658               return 0;
659           break;
660
661         case 'e':
662           if (rtx_equal_p (XEXP (x, i), XEXP (y, i)) == 0)
663             return 0;
664           break;
665
666         case 'S':
667         case 's':
668           if (strcmp (XSTR (x, i), XSTR (y, i)))
669             return 0;
670           break;
671
672         case 'u':
673           /* These are just backpointers, so they don't matter.  */
674           break;
675
676         case '0':
677         case 't':
678           break;
679
680           /* It is believed that rtx's at this level will never
681              contain anything but integers and other rtx's,
682              except for within LABEL_REFs and SYMBOL_REFs.  */
683         default:
684           abort ();
685         }
686     }
687   return 1;
688 }
689 \f
690 /* Subroutines of read_rtx.  */
691
692 /* The current line number for the file.  */
693 int read_rtx_lineno = 1;
694
695 /* The filename for aborting with file and line.  */
696 const char *read_rtx_filename = "<unknown>";
697
698 static void
699 fatal_with_file_and_line VPARAMS ((FILE *infile, const char *msg, ...))
700 {
701 #ifndef ANSI_PROTOTYPES
702   FILE *infile;
703   const char *msg;
704 #endif
705   va_list ap;
706   char context[64];
707   size_t i;
708   int c;
709
710   VA_START (ap, msg);
711
712 #ifndef ANSI_PROTOTYPES
713   infile = va_arg (ap, FILE *);
714   msg = va_arg (ap, const char *);
715 #endif
716
717   fprintf (stderr, "%s:%d: ", read_rtx_filename, read_rtx_lineno);
718   vfprintf (stderr, msg, ap);
719   putc ('\n', stderr);
720
721   /* Gather some following context.  */
722   for (i = 0; i < sizeof(context)-1; ++i)
723     {
724       c = getc (infile);
725       if (c == EOF)
726         break;
727       if (c == '\r' || c == '\n')
728         break;
729       context[i] = c;
730     }
731   context[i] = '\0';
732
733   fprintf (stderr, "%s:%d: following context is `%s'\n",
734            read_rtx_filename, read_rtx_lineno, context);
735
736   va_end (ap);
737   exit (1);
738 }
739
740 /* Dump code after printing a message.  Used when read_rtx finds
741    invalid data.  */
742
743 static void
744 fatal_expected_char (infile, expected_c, actual_c)
745      FILE *infile;
746      int expected_c, actual_c;
747 {
748   fatal_with_file_and_line (infile, "expected character `%c', found `%c'",
749                             expected_c, actual_c);
750 }
751
752 /* Read chars from INFILE until a non-whitespace char
753    and return that.  Comments, both Lisp style and C style,
754    are treated as whitespace.
755    Tools such as genflags use this function.  */
756
757 int
758 read_skip_spaces (infile)
759      FILE *infile;
760 {
761   register int c;
762   while (1)
763     {
764       c = getc (infile);
765       switch (c)
766         {
767         case '\n':
768           read_rtx_lineno++;
769           break;
770
771         case ' ': case '\t': case '\f': case '\r':
772           break;
773
774         case ';':
775           do
776             c = getc (infile);
777           while (c != '\n' && c != EOF);
778           read_rtx_lineno++;
779           break;
780
781         case '/':
782           {
783             register int prevc;
784             c = getc (infile);
785             if (c != '*')
786               fatal_expected_char (infile, '*', c);
787
788             prevc = 0;
789             while ((c = getc (infile)) && c != EOF)
790               {
791                 if (c == '\n')
792                    read_rtx_lineno++;
793                 else if (prevc == '*' && c == '/')
794                   break;
795                 prevc = c;
796               }
797           }
798           break;
799
800         default:
801           return c;
802         }
803     }
804 }
805
806 /* Read an rtx code name into the buffer STR[].
807    It is terminated by any of the punctuation chars of rtx printed syntax.  */
808
809 static void
810 read_name (str, infile)
811      char *str;
812      FILE *infile;
813 {
814   register char *p;
815   register int c;
816
817   c = read_skip_spaces(infile);
818
819   p = str;
820   while (1)
821     {
822       if (c == ' ' || c == '\n' || c == '\t' || c == '\f')
823         break;
824       if (c == ':' || c == ')' || c == ']' || c == '"' || c == '/'
825           || c == '(' || c == '[')
826         {
827           ungetc (c, infile);
828           break;
829         }
830       *p++ = c;
831       c = getc (infile);
832     }
833   if (p == str)
834     fatal_with_file_and_line (infile, "missing name or number");
835   if (c == '\n')
836     read_rtx_lineno++;
837
838   *p = 0;
839
840   if (md_constants)
841     {
842       /* Do constant expansion.  */
843       struct md_constant *def;
844
845       p = str;
846       do
847         {
848           struct md_constant tmp_def;
849
850           tmp_def.name = p;
851           def = htab_find (md_constants, &tmp_def);
852           if (def)
853             p = def->value;
854         } while (def);
855       if (p != str)
856         strcpy (str, p);
857     }
858 }
859 \f
860 /* Provide a version of a function to read a long long if the system does
861    not provide one.  */
862 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !defined(HAVE_ATOLL) && !defined(HAVE_ATOQ)
863 HOST_WIDE_INT
864 atoll(p)
865     const char *p;
866 {
867   int neg = 0;
868   HOST_WIDE_INT tmp_wide;
869
870   while (ISSPACE(*p))
871     p++;
872   if (*p == '-')
873     neg = 1, p++;
874   else if (*p == '+')
875     p++;
876
877   tmp_wide = 0;
878   while (ISDIGIT(*p))
879     {
880       HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
881       if (new_wide < tmp_wide)
882         {
883           /* Return INT_MAX equiv on overflow.  */
884           tmp_wide = (~(unsigned HOST_WIDE_INT)0) >> 1;
885           break;
886         }
887       tmp_wide = new_wide;
888       p++;
889     }
890
891   if (neg)
892     tmp_wide = -tmp_wide;
893   return tmp_wide;
894 }
895 #endif
896
897 /* Given a constant definition, return a hash code for its name.  */
898 static unsigned
899 def_hash (def)
900      const void *def;
901 {
902   unsigned result, i;
903   const char *string = ((const struct md_constant *)def)->name;
904
905   for (result = i = 0;*string++ != '\0'; i++)
906     result += ((unsigned char) *string << (i % CHAR_BIT));
907   return result;
908 }
909
910 /* Given two constant definitions, return true if they have the same name.  */
911 static int
912 def_name_eq_p (def1, def2)
913      const void *def1, *def2;
914 {
915   return ! strcmp (((const struct md_constant *)def1)->name,
916                    ((const struct md_constant *)def2)->name);
917 }
918
919 /* INFILE is a FILE pointer to read text from.  TMP_CHAR is a buffer suitable
920    to read a name or number into.  Process a define_constants directive,
921    starting with the optional space after the "define_constants".  */
922 static void
923 read_constants (infile, tmp_char)
924      FILE *infile;
925      char *tmp_char;
926 {
927   int c;
928   htab_t defs;
929
930   c = read_skip_spaces (infile);
931   if (c != '[')
932     fatal_expected_char (infile, '[', c);
933   defs = md_constants;
934   if (! defs)
935     defs = htab_create (32, def_hash, def_name_eq_p, (htab_del) 0);
936   /* Disable constant expansion during definition processing.  */
937   md_constants = 0;
938   while ( (c = read_skip_spaces (infile)) != ']')
939     {
940       struct md_constant *def;
941       void **entry_ptr;
942
943       if (c != '(')
944         fatal_expected_char (infile, '(', c);
945       def = xmalloc (sizeof (struct md_constant));
946       def->name = tmp_char;
947       read_name (tmp_char, infile);
948       entry_ptr = htab_find_slot (defs, def, TRUE);
949       if (! *entry_ptr)
950         def->name = xstrdup (tmp_char);
951       c = read_skip_spaces (infile);
952       ungetc (c, infile);
953       read_name (tmp_char, infile);
954       if (! *entry_ptr)
955         {
956           def->value = xstrdup (tmp_char);
957           *entry_ptr = def;
958         }
959       else
960         {
961           def = *entry_ptr;
962           if (strcmp (def->value, tmp_char))
963             fatal_with_file_and_line (infile,
964                                       "redefinition of %s, was %s, now %s",
965                                       def->name, def->value, tmp_char);
966         }
967       c = read_skip_spaces (infile);
968       if (c != ')')
969         fatal_expected_char (infile, ')', c);
970     }
971   md_constants = defs;
972   c = read_skip_spaces (infile);
973   if (c != ')')
974     fatal_expected_char (infile, ')', c);
975 }
976
977 /* For every constant definition, call CALLBACK with two arguments:
978    a pointer a pointer to the constant definition and INFO.
979    Stops when CALLBACK returns zero.  */
980 void
981 traverse_md_constants (callback, info)
982      htab_trav callback;
983      void *info;
984 {
985   if (md_constants)
986     htab_traverse (md_constants, callback, info);
987 }
988
989 /* Read an rtx in printed representation from INFILE
990    and return an actual rtx in core constructed accordingly.
991    read_rtx is not used in the compiler proper, but rather in
992    the utilities gen*.c that construct C code from machine descriptions.  */
993
994 rtx
995 read_rtx (infile)
996      FILE *infile;
997 {
998   register int i, j, list_counter;
999   RTX_CODE tmp_code;
1000   register const char *format_ptr;
1001   /* tmp_char is a buffer used for reading decimal integers
1002      and names of rtx types and machine modes.
1003      Therefore, 256 must be enough.  */
1004   char tmp_char[256];
1005   rtx return_rtx;
1006   register int c;
1007   int tmp_int;
1008   HOST_WIDE_INT tmp_wide;
1009
1010   /* Obstack used for allocating RTL objects.  */
1011   static struct obstack rtl_obstack;
1012   static int initialized;
1013
1014   /* Linked list structure for making RTXs: */
1015   struct rtx_list
1016     {
1017       struct rtx_list *next;
1018       rtx value;                /* Value of this node.  */
1019     };
1020
1021   if (!initialized) {
1022     _obstack_begin (&rtl_obstack,0, 0,
1023                     (void *(*) PARAMS ((long))) xmalloc,
1024                     (void (*) PARAMS ((void *))) free);
1025     initialized = 1;
1026   }
1027
1028 again:
1029   c = read_skip_spaces (infile); /* Should be open paren.  */
1030   if (c != '(')
1031     fatal_expected_char (infile, '(', c);
1032
1033   read_name (tmp_char, infile);
1034
1035   tmp_code = UNKNOWN;
1036
1037   if (! strcmp (tmp_char, "define_constants"))
1038     {
1039       read_constants (infile, tmp_char);
1040       goto again;
1041     }
1042   for (i = 0; i < NUM_RTX_CODE; i++)
1043     if (! strcmp (tmp_char, GET_RTX_NAME (i)))
1044       {
1045         tmp_code = (RTX_CODE) i;        /* get value for name */
1046         break;
1047       }
1048
1049   if (tmp_code == UNKNOWN)
1050     fatal_with_file_and_line (infile, "unknown rtx code `%s'", tmp_char);
1051
1052   /* (NIL) stands for an expression that isn't there.  */
1053   if (tmp_code == NIL)
1054     {
1055       /* Discard the closeparen.  */
1056       while ((c = getc (infile)) && c != ')')
1057         ;
1058
1059       return 0;
1060     }
1061
1062   /* If we end up with an insn expression then we free this space below.  */
1063   return_rtx = rtx_alloc (tmp_code);
1064   format_ptr = GET_RTX_FORMAT (GET_CODE (return_rtx));
1065
1066   /* If what follows is `: mode ', read it and
1067      store the mode in the rtx.  */
1068
1069   i = read_skip_spaces (infile);
1070   if (i == ':')
1071     {
1072       read_name (tmp_char, infile);
1073       for (j = 0; j < NUM_MACHINE_MODES; j++)
1074         if (! strcmp (GET_MODE_NAME (j), tmp_char))
1075           break;
1076
1077       if (j == MAX_MACHINE_MODE)
1078         fatal_with_file_and_line (infile, "unknown mode `%s'", tmp_char);
1079
1080       PUT_MODE (return_rtx, (enum machine_mode) j);
1081     }
1082   else
1083     ungetc (i, infile);
1084
1085   for (i = 0; i < GET_RTX_LENGTH (GET_CODE (return_rtx)); i++)
1086     switch (*format_ptr++)
1087       {
1088         /* 0 means a field for internal use only.
1089            Don't expect it to be present in the input.  */
1090       case '0':
1091         break;
1092
1093       case 'e':
1094       case 'u':
1095         XEXP (return_rtx, i) = read_rtx (infile);
1096         break;
1097
1098       case 'V':
1099         /* 'V' is an optional vector: if a closeparen follows,
1100            just store NULL for this element.  */
1101         c = read_skip_spaces (infile);
1102         ungetc (c, infile);
1103         if (c == ')')
1104           {
1105             XVEC (return_rtx, i) = 0;
1106             break;
1107           }
1108         /* Now process the vector.  */
1109
1110       case 'E':
1111         {
1112           register struct rtx_list *next_rtx, *rtx_list_link;
1113           struct rtx_list *list_rtx = NULL;
1114
1115           c = read_skip_spaces (infile);
1116           if (c != '[')
1117             fatal_expected_char (infile, '[', c);
1118
1119           /* add expressions to a list, while keeping a count */
1120           next_rtx = NULL;
1121           list_counter = 0;
1122           while ((c = read_skip_spaces (infile)) && c != ']')
1123             {
1124               ungetc (c, infile);
1125               list_counter++;
1126               rtx_list_link = (struct rtx_list *)
1127                 alloca (sizeof (struct rtx_list));
1128               rtx_list_link->value = read_rtx (infile);
1129               if (next_rtx == 0)
1130                 list_rtx = rtx_list_link;
1131               else
1132                 next_rtx->next = rtx_list_link;
1133               next_rtx = rtx_list_link;
1134               rtx_list_link->next = 0;
1135             }
1136           /* get vector length and allocate it */
1137           XVEC (return_rtx, i) = (list_counter
1138                                   ? rtvec_alloc (list_counter) : NULL_RTVEC);
1139           if (list_counter > 0)
1140             {
1141               next_rtx = list_rtx;
1142               for (j = 0; j < list_counter; j++,
1143                    next_rtx = next_rtx->next)
1144                 XVECEXP (return_rtx, i, j) = next_rtx->value;
1145             }
1146           /* close bracket gotten */
1147         }
1148         break;
1149
1150       case 'S':
1151         /* 'S' is an optional string: if a closeparen follows,
1152            just store NULL for this element.  */
1153         c = read_skip_spaces (infile);
1154         ungetc (c, infile);
1155         if (c == ')')
1156           {
1157             XSTR (return_rtx, i) = 0;
1158             break;
1159           }
1160
1161       case 's':
1162         {
1163           int saw_paren = 0;
1164           register char *stringbuf;
1165           int saw_anything = 0;
1166
1167           c = read_skip_spaces (infile);
1168           if (c == '(')
1169             {
1170               saw_paren = 1;
1171               c = read_skip_spaces (infile);
1172             }
1173           if (c != '"')
1174             fatal_expected_char (infile, '"', c);
1175
1176           while (1)
1177             {
1178               c = getc (infile); /* Read the string  */
1179               if (c == '\n')
1180                 read_rtx_lineno++;
1181               if (c == '\\')
1182                 {
1183                   c = getc (infile);    /* Read the string  */
1184                   /* \; makes stuff for a C string constant containing
1185                      newline and tab.  */
1186                   if (c == ';')
1187                     {
1188                       obstack_grow (&rtl_obstack, "\\n\\t", 4);
1189                       continue;
1190                     }
1191                   if (c == '\n')
1192                     read_rtx_lineno++;
1193                 }
1194               else if (c == '"')
1195                 break;
1196
1197               obstack_1grow (&rtl_obstack, c);
1198               saw_anything = 1;
1199             }
1200
1201           /* For insn patterns, we want to provide a default name
1202              based on the file and line, like "*foo.md:12", if the
1203              given name is blank.  These are only for define_insn and
1204              define_insn_and_split, to aid debugging.  */
1205           if (!saw_anything
1206               && i == 0
1207               && (GET_CODE (return_rtx) == DEFINE_INSN
1208                   || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT))
1209             {
1210               char line_name[20];
1211               const char *fn = (read_rtx_filename ? read_rtx_filename : "rtx");
1212               const char *slash;
1213               for (slash = fn; *slash; slash ++)
1214                 if (*slash == '/' || *slash == '\\' || *slash == ':')
1215                   fn = slash + 1;
1216               obstack_1grow (&rtl_obstack, '*');
1217               obstack_grow (&rtl_obstack, fn, strlen (fn));
1218               sprintf (line_name, ":%d", read_rtx_lineno);
1219               obstack_grow (&rtl_obstack, line_name, strlen (line_name));
1220             }
1221
1222           obstack_1grow (&rtl_obstack, 0);
1223           stringbuf = (char *) obstack_finish (&rtl_obstack);
1224
1225           if (saw_paren)
1226             {
1227               c = read_skip_spaces (infile);
1228               if (c != ')')
1229                 fatal_expected_char (infile, ')', c);
1230             }
1231           XSTR (return_rtx, i) = stringbuf;
1232         }
1233         break;
1234
1235       case 'w':
1236         read_name (tmp_char, infile);
1237 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1238         tmp_wide = atoi (tmp_char);
1239 #else
1240 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1241         tmp_wide = atol (tmp_char);
1242 #else
1243         /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1244            But prefer not to use our hand-rolled function above either.  */
1245 #if defined(HAVE_ATOLL) || !defined(HAVE_ATOQ)
1246         tmp_wide = atoll (tmp_char);
1247 #else
1248         tmp_wide = atoq (tmp_char);
1249 #endif
1250 #endif
1251 #endif
1252         XWINT (return_rtx, i) = tmp_wide;
1253         break;
1254
1255       case 'i':
1256       case 'n':
1257         read_name (tmp_char, infile);
1258         tmp_int = atoi (tmp_char);
1259         XINT (return_rtx, i) = tmp_int;
1260         break;
1261
1262       default:
1263         fprintf (stderr,
1264                  "switch format wrong in rtl.read_rtx(). format was: %c.\n",
1265                  format_ptr[-1]);
1266         fprintf (stderr, "\tfile position: %ld\n", ftell (infile));
1267         abort ();
1268       }
1269
1270   c = read_skip_spaces (infile);
1271   if (c != ')')
1272     fatal_expected_char (infile, ')', c);
1273
1274   return return_rtx;
1275 }
1276
1277 #if defined ENABLE_RTL_CHECKING && (GCC_VERSION >= 2007)
1278 void
1279 rtl_check_failed_bounds (r, n, file, line, func)
1280     rtx r;
1281     int n;
1282     const char *file;
1283     int line;
1284     const char *func;
1285 {
1286   error ("RTL check: access of elt %d of `%s' with last elt %d",
1287          n, GET_RTX_NAME (GET_CODE (r)), GET_RTX_LENGTH (GET_CODE (r))-1);
1288   fancy_abort (file, line, func);
1289 }
1290
1291 void
1292 rtl_check_failed_type1 (r, n, c1, file, line, func)
1293     rtx r;
1294     int n;
1295     int c1;
1296     const char *file;
1297     int line;
1298     const char *func;
1299 {
1300   error ("RTL check: expected elt %d type '%c', have '%c' (rtx %s)",
1301          n, c1, GET_RTX_FORMAT (GET_CODE (r))[n], GET_RTX_NAME (GET_CODE (r)));
1302   fancy_abort (file, line, func);
1303 }
1304
1305 void
1306 rtl_check_failed_type2 (r, n, c1, c2, file, line, func)
1307     rtx r;
1308     int n;
1309     int c1;
1310     int c2;
1311     const char *file;
1312     int line;
1313     const char *func;
1314 {
1315   error ("RTL check: expected elt %d type '%c' or '%c', have '%c' (rtx %s)",
1316          n, c1, c2,
1317          GET_RTX_FORMAT (GET_CODE (r))[n], GET_RTX_NAME (GET_CODE(r)));
1318   fancy_abort (file, line, func);
1319 }
1320
1321 void
1322 rtl_check_failed_code1 (r, code, file, line, func)
1323     rtx r;
1324     enum rtx_code code;
1325     const char *file;
1326     int line;
1327     const char *func;
1328 {
1329   error ("RTL check: expected code `%s', have `%s'",
1330          GET_RTX_NAME (code), GET_RTX_NAME (GET_CODE (r)));
1331   fancy_abort (file, line, func);
1332 }
1333
1334 void
1335 rtl_check_failed_code2 (r, code1, code2, file, line, func)
1336     rtx r;
1337     enum rtx_code code1, code2;
1338     const char *file;
1339     int line;
1340     const char *func;
1341 {
1342   error ("RTL check: expected code `%s' or `%s', have `%s'",
1343          GET_RTX_NAME (code1), GET_RTX_NAME (code2),
1344          GET_RTX_NAME (GET_CODE (r)));
1345   fancy_abort (file, line, func);
1346 }
1347
1348 /* XXX Maybe print the vector?  */
1349 void
1350 rtvec_check_failed_bounds (r, n, file, line, func)
1351     rtvec r;
1352     int n;
1353     const char *file;
1354     int line;
1355     const char *func;
1356 {
1357   error ("RTL check: access of elt %d of vector with last elt %d",
1358          n, GET_NUM_ELEM (r)-1);
1359   fancy_abort (file, line, func);
1360 }
1361 #endif /* ENABLE_RTL_CHECKING */