OSDN Git Service

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