OSDN Git Service

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