OSDN Git Service

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