OSDN Git Service

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