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