OSDN Git Service

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