remove extraneous code checked in with previous delta
[official-gcc.git] / gcc / rtl.c
bloba8b1a9de63ac6e6fb140adfc64989b70600164a1
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.
5 This file is part of GNU CC.
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.
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.
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. */
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"
32 #define obstack_chunk_alloc xmalloc
33 #define obstack_chunk_free free
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. */
41 extern struct obstack *rtl_obstack;
43 /* Calculate the format for CONST_DOUBLE. This depends on the relative
44 widths of HOST_WIDE_INT and REAL_VALUE_TYPE.
46 We need to go out to e0wwwww, since REAL_ARITHMETIC assumes 16-bits
47 per element in REAL_VALUE_TYPE.
49 This is duplicated in gengenrtl.c.
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. */
54 #ifdef REAL_ARITHMETIC
55 #if MAX_LONG_DOUBLE_TYPE_SIZE == 96
56 #define REAL_WIDTH (11*8 + HOST_BITS_PER_WIDE_INT)/HOST_BITS_PER_WIDE_INT
57 #elif MAX_LONG_DOUBLE_TYPE_SIZE == 128
58 #define REAL_WIDTH (19*8 + HOST_BITS_PER_WIDE_INT)/HOST_BITS_PER_WIDE_INT
59 #elif HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT
60 #define REAL_WIDTH (7*8 + HOST_BITS_PER_WIDE_INT)/HOST_BITS_PER_WIDE_INT
61 #endif
62 #endif /* REAL_ARITHMETIC */
64 #ifndef REAL_WIDTH
65 #if HOST_BITS_PER_WIDE_INT*2 >= MAX_LONG_DOUBLE_TYPE_SIZE
66 #define REAL_WIDTH 2
67 #elif HOST_BITS_PER_WIDE_INT*3 >= MAX_LONG_DOUBLE_TYPE_SIZE
68 #define REAL_WIDTH 3
69 #elif HOST_BITS_PER_WIDE_INT*4 >= MAX_LONG_DOUBLE_TYPE_SIZE
70 #define REAL_WIDTH 4
71 #endif
72 #endif /* REAL_WIDTH */
74 #if REAL_WIDTH == 1
75 #define CONST_DOUBLE_FORMAT "e0ww"
76 #elif REAL_WIDTH == 2
77 #define CONST_DOUBLE_FORMAT "e0ww"
78 #elif REAL_WIDTH == 3
79 #define CONST_DOUBLE_FORMAT "e0www"
80 #elif REAL_WIDTH == 4
81 #define CONST_DOUBLE_FORMAT "e0wwww"
82 #elif REAL_WIDTH == 5
83 #define CONST_DOUBLE_FORMAT "e0wwwww"
84 #else
85 #define CONST_DOUBLE_FORMAT /* nothing - will cause syntax error */
86 #endif
88 /* Indexed by rtx code, gives number of operands for an rtx with that code.
89 Does NOT include rtx header data (code and links). */
91 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) sizeof FORMAT - 1 ,
93 const int rtx_length[NUM_RTX_CODE + 1] = {
94 #include "rtl.def"
97 #undef DEF_RTL_EXPR
99 /* Indexed by rtx code, gives the name of that kind of rtx, as a C string. */
101 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
103 const char * const rtx_name[] = {
104 #include "rtl.def" /* rtl expressions are documented here */
107 #undef DEF_RTL_EXPR
109 /* Indexed by machine mode, gives the name of that machine mode.
110 This name does not include the letters "mode". */
112 #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT, WIDER) NAME,
114 const char * const mode_name[(int) MAX_MACHINE_MODE + 1] = {
115 #include "machmode.def"
116 /* Add an extra field to avoid a core dump if someone tries to convert
117 MAX_MACHINE_MODE to a string. */
121 #undef DEF_MACHMODE
123 /* Indexed by machine mode, gives the length of the mode, in bytes.
124 GET_MODE_CLASS uses this. */
126 #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT, WIDER) CLASS,
128 const enum mode_class mode_class[(int) MAX_MACHINE_MODE] = {
129 #include "machmode.def"
132 #undef DEF_MACHMODE
134 /* Indexed by machine mode, gives the length of the mode, in bytes.
135 GET_MODE_SIZE uses this. */
137 #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT, WIDER) SIZE,
139 const int mode_size[(int) MAX_MACHINE_MODE] = {
140 #include "machmode.def"
143 #undef DEF_MACHMODE
145 /* Indexed by machine mode, gives the length of the mode's subunit.
146 GET_MODE_UNIT_SIZE uses this. */
148 #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT, WIDER) UNIT,
150 const int mode_unit_size[(int) MAX_MACHINE_MODE] = {
151 #include "machmode.def" /* machine modes are documented here */
154 #undef DEF_MACHMODE
156 /* Indexed by machine mode, gives next wider natural mode
157 (QI -> HI -> SI -> DI, etc.) Widening multiply instructions
158 use this. */
160 #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT, WIDER) \
161 (unsigned char) WIDER,
163 const unsigned char mode_wider_mode[(int) MAX_MACHINE_MODE] = {
164 #include "machmode.def" /* machine modes are documented here */
167 #undef DEF_MACHMODE
169 #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT, WIDER) \
170 ((SIZE) * BITS_PER_UNIT >= HOST_BITS_PER_WIDE_INT) ? ~(unsigned HOST_WIDE_INT)0 : ((unsigned HOST_WIDE_INT) 1 << (SIZE) * BITS_PER_UNIT) - 1,
172 /* Indexed by machine mode, gives mask of significant bits in mode. */
174 const unsigned HOST_WIDE_INT mode_mask_array[(int) MAX_MACHINE_MODE] = {
175 #include "machmode.def"
178 /* Indexed by mode class, gives the narrowest mode for each class.
179 The Q modes are always of width 1 (2 for complex) - it is impossible
180 for any mode to be narrower. */
182 const enum machine_mode class_narrowest_mode[(int) MAX_MODE_CLASS] = {
183 /* MODE_RANDOM */ VOIDmode,
184 /* MODE_INT */ QImode,
185 /* MODE_FLOAT */ QFmode,
186 /* MODE_PARTIAL_INT */ PQImode,
187 /* MODE_CC */ CCmode,
188 /* MODE_COMPLEX_INT */ CQImode,
189 /* MODE_COMPLEX_FLOAT */ QCmode
193 /* Indexed by rtx code, gives a sequence of operand-types for
194 rtx's of that code. The sequence is a C string in which
195 each character describes one operand. */
197 const char * const rtx_format[] = {
198 /* "*" undefined.
199 can cause a warning message
200 "0" field is unused (or used in a phase-dependent manner)
201 prints nothing
202 "i" an integer
203 prints the integer
204 "n" like "i", but prints entries from `note_insn_name'
205 "w" an integer of width HOST_BITS_PER_WIDE_INT
206 prints the integer
207 "s" a pointer to a string
208 prints the string
209 "S" like "s", but optional:
210 the containing rtx may end before this operand
211 "e" a pointer to an rtl expression
212 prints the expression
213 "E" a pointer to a vector that points to a number of rtl expressions
214 prints a list of the rtl expressions
215 "V" like "E", but optional:
216 the containing rtx may end before this operand
217 "u" a pointer to another insn
218 prints the uid of the insn.
219 "b" is a pointer to a bitmap header.
220 "t" is a tree pointer. */
222 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
223 #include "rtl.def" /* rtl expressions are defined here */
224 #undef DEF_RTL_EXPR
227 /* Indexed by rtx code, gives a character representing the "class" of
228 that rtx code. See rtl.def for documentation on the defined classes. */
230 const char rtx_class[] = {
231 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) CLASS,
232 #include "rtl.def" /* rtl expressions are defined here */
233 #undef DEF_RTL_EXPR
236 /* Names for kinds of NOTEs and REG_NOTEs. */
238 const char * const note_insn_name[] = { 0 , "NOTE_INSN_DELETED",
239 "NOTE_INSN_BLOCK_BEG", "NOTE_INSN_BLOCK_END",
240 "NOTE_INSN_LOOP_BEG", "NOTE_INSN_LOOP_END",
241 "NOTE_INSN_FUNCTION_END", "NOTE_INSN_SETJMP",
242 "NOTE_INSN_LOOP_CONT", "NOTE_INSN_LOOP_VTOP",
243 "NOTE_INSN_PROLOGUE_END", "NOTE_INSN_EPILOGUE_BEG",
244 "NOTE_INSN_DELETED_LABEL", "NOTE_INSN_FUNCTION_BEG",
245 "NOTE_INSN_EH_REGION_BEG", "NOTE_INSN_EH_REGION_END",
246 "NOTE_REPEATED_LINE_NUMBER", "NOTE_INSN_RANGE_START",
247 "NOTE_INSN_RANGE_END", "NOTE_INSN_LIVE",
248 "NOTE_INSN_BASIC_BLOCK" };
250 const char * const reg_note_name[] = { "", "REG_DEAD", "REG_INC", "REG_EQUIV", "REG_WAS_0",
251 "REG_EQUAL", "REG_RETVAL", "REG_LIBCALL",
252 "REG_NONNEG", "REG_NO_CONFLICT", "REG_UNUSED",
253 "REG_CC_SETTER", "REG_CC_USER", "REG_LABEL",
254 "REG_DEP_ANTI", "REG_DEP_OUTPUT", "REG_BR_PROB",
255 "REG_EXEC_COUNT", "REG_NOALIAS", "REG_SAVE_AREA",
256 "REG_BR_PRED", "REG_EH_CONTEXT",
257 "REG_FRAME_RELATED_EXPR", "REG_EH_REGION",
258 "REG_EH_RETHROW", "REG_SAVE_NOTE" };
260 static void fatal_with_file_and_line PARAMS ((FILE *, const char *, ...))
261 ATTRIBUTE_PRINTF_2 ATTRIBUTE_NORETURN;
262 static void fatal_expected_char PARAMS ((FILE *, int, int)) ATTRIBUTE_NORETURN;
263 static void read_name PARAMS ((char *, FILE *));
264 static const char *trim_filename PARAMS ((const char *));
266 /* Allocate an rtx vector of N elements.
267 Store the length, and initialize all elements to zero. */
269 rtvec
270 rtvec_alloc (n)
271 int n;
273 rtvec rt;
275 if (ggc_p)
276 rt = ggc_alloc_rtvec (n);
277 else
279 int i;
281 rt = (rtvec) obstack_alloc (rtl_obstack,
282 sizeof (struct rtvec_def)
283 + (( n - 1) * sizeof (rtx)));
285 /* clear out the vector */
286 for (i = 0; i < n; i++)
287 rt->elem[i] = 0;
290 PUT_NUM_ELEM (rt, n);
291 return rt;
294 /* Allocate an rtx of code CODE. The CODE is stored in the rtx;
295 all the rest is initialized to zero. */
298 rtx_alloc (code)
299 RTX_CODE code;
301 rtx rt;
303 if (ggc_p)
304 rt = ggc_alloc_rtx (GET_RTX_LENGTH (code));
305 else
307 register struct obstack *ob = rtl_obstack;
308 register int nelts = GET_RTX_LENGTH (code);
309 register int length = sizeof (struct rtx_def)
310 + (nelts - 1) * sizeof (rtunion);
312 /* This function is called more than any other in GCC, so we
313 manipulate the obstack directly.
315 Even though rtx objects are word aligned, we may be sharing
316 an obstack with tree nodes, which may have to be double-word
317 aligned. So align our length to the alignment mask in the
318 obstack. */
320 length = (length + ob->alignment_mask) & ~ ob->alignment_mask;
322 if (ob->chunk_limit - ob->next_free < length)
323 _obstack_newchunk (ob, length);
324 rt = (rtx)ob->object_base;
325 ob->next_free += length;
326 ob->object_base = ob->next_free;
328 /* We want to clear everything up to the FLD array. Normally,
329 this is one int, but we don't want to assume that and it
330 isn't very portable anyway; this is. */
332 memset (rt, 0, sizeof (struct rtx_def) - sizeof (rtunion));
335 PUT_CODE (rt, code);
336 return rt;
339 /* Free the rtx X and all RTL allocated since X. */
341 void
342 rtx_free (x)
343 rtx x;
345 if (!ggc_p)
346 obstack_free (rtl_obstack, x);
349 /* Create a new copy of an rtx.
350 Recursively copies the operands of the rtx,
351 except for those few rtx codes that are sharable. */
354 copy_rtx (orig)
355 register rtx orig;
357 register rtx copy;
358 register int i, j;
359 register RTX_CODE code;
360 register const char *format_ptr;
362 code = GET_CODE (orig);
364 switch (code)
366 case REG:
367 case QUEUED:
368 case CONST_INT:
369 case CONST_DOUBLE:
370 case SYMBOL_REF:
371 case CODE_LABEL:
372 case PC:
373 case CC0:
374 case SCRATCH:
375 /* SCRATCH must be shared because they represent distinct values. */
376 case ADDRESSOF:
377 return orig;
379 case CONST:
380 /* CONST can be shared if it contains a SYMBOL_REF. If it contains
381 a LABEL_REF, it isn't sharable. */
382 if (GET_CODE (XEXP (orig, 0)) == PLUS
383 && GET_CODE (XEXP (XEXP (orig, 0), 0)) == SYMBOL_REF
384 && GET_CODE (XEXP (XEXP (orig, 0), 1)) == CONST_INT)
385 return orig;
386 break;
388 /* A MEM with a constant address is not sharable. The problem is that
389 the constant address may need to be reloaded. If the mem is shared,
390 then reloading one copy of this mem will cause all copies to appear
391 to have been reloaded. */
393 default:
394 break;
397 copy = rtx_alloc (code);
399 /* Copy the various flags, and other information. We assume that
400 all fields need copying, and then clear the fields that should
401 not be copied. That is the sensible default behavior, and forces
402 us to explicitly document why we are *not* copying a flag. */
403 memcpy (copy, orig, sizeof (struct rtx_def) - sizeof (rtunion));
405 /* We do not copy the USED flag, which is used as a mark bit during
406 walks over the RTL. */
407 copy->used = 0;
409 /* We do not copy FRAME_RELATED for INSNs. */
410 if (GET_RTX_CLASS (code) == 'i')
411 copy->frame_related = 0;
412 copy->jump = orig->jump;
413 copy->call = orig->call;
415 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
417 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
419 copy->fld[i] = orig->fld[i];
420 switch (*format_ptr++)
422 case 'e':
423 if (XEXP (orig, i) != NULL)
424 XEXP (copy, i) = copy_rtx (XEXP (orig, i));
425 break;
427 case 'E':
428 case 'V':
429 if (XVEC (orig, i) != NULL)
431 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
432 for (j = 0; j < XVECLEN (copy, i); j++)
433 XVECEXP (copy, i, j) = copy_rtx (XVECEXP (orig, i, j));
435 break;
437 case 'b':
439 bitmap new_bits = BITMAP_OBSTACK_ALLOC (rtl_obstack);
440 bitmap_copy (new_bits, XBITMAP (orig, i));
441 XBITMAP (copy, i) = new_bits;
442 break;
445 case 't':
446 case 'w':
447 case 'i':
448 case 's':
449 case 'S':
450 case 'u':
451 case '0':
452 /* These are left unchanged. */
453 break;
455 default:
456 abort ();
459 return copy;
462 /* Similar to `copy_rtx' except that if MAY_SHARE is present, it is
463 placed in the result directly, rather than being copied. */
466 copy_most_rtx (orig, may_share)
467 register rtx orig;
468 register rtx may_share;
470 register rtx copy;
471 register int i, j;
472 register RTX_CODE code;
473 register const char *format_ptr;
475 if (orig == may_share)
476 return orig;
478 code = GET_CODE (orig);
480 switch (code)
482 case REG:
483 case QUEUED:
484 case CONST_INT:
485 case CONST_DOUBLE:
486 case SYMBOL_REF:
487 case CODE_LABEL:
488 case PC:
489 case CC0:
490 return orig;
491 default:
492 break;
495 copy = rtx_alloc (code);
496 PUT_MODE (copy, GET_MODE (orig));
497 copy->in_struct = orig->in_struct;
498 copy->volatil = orig->volatil;
499 copy->unchanging = orig->unchanging;
500 copy->integrated = orig->integrated;
502 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
504 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
506 switch (*format_ptr++)
508 case 'e':
509 XEXP (copy, i) = XEXP (orig, i);
510 if (XEXP (orig, i) != NULL && XEXP (orig, i) != may_share)
511 XEXP (copy, i) = copy_most_rtx (XEXP (orig, i), may_share);
512 break;
514 case 'u':
515 XEXP (copy, i) = XEXP (orig, i);
516 break;
518 case 'E':
519 case 'V':
520 XVEC (copy, i) = XVEC (orig, i);
521 if (XVEC (orig, i) != NULL)
523 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
524 for (j = 0; j < XVECLEN (copy, i); j++)
525 XVECEXP (copy, i, j)
526 = copy_most_rtx (XVECEXP (orig, i, j), may_share);
528 break;
530 case 'w':
531 XWINT (copy, i) = XWINT (orig, i);
532 break;
534 case 'n':
535 case 'i':
536 XINT (copy, i) = XINT (orig, i);
537 break;
539 case 't':
540 XTREE (copy, i) = XTREE (orig, i);
541 break;
543 case 's':
544 case 'S':
545 XSTR (copy, i) = XSTR (orig, i);
546 break;
548 case '0':
549 /* Copy this through the wide int field; that's safest. */
550 X0WINT (copy, i) = X0WINT (orig, i);
551 break;
553 default:
554 abort ();
557 return copy;
560 /* Create a new copy of an rtx. Only copy just one level. */
562 shallow_copy_rtx (orig)
563 rtx orig;
565 register int i;
566 register RTX_CODE code = GET_CODE (orig);
567 register rtx copy = rtx_alloc (code);
569 PUT_MODE (copy, GET_MODE (orig));
570 copy->in_struct = orig->in_struct;
571 copy->volatil = orig->volatil;
572 copy->unchanging = orig->unchanging;
573 copy->integrated = orig->integrated;
575 for (i = 0; i < GET_RTX_LENGTH (code); i++)
576 copy->fld[i] = orig->fld[i];
578 return copy;
581 /* This is 1 until after the rtl generation pass. */
582 int rtx_equal_function_value_matters;
584 /* Return 1 if X and Y are identical-looking rtx's.
585 This is the Lisp function EQUAL for rtx arguments. */
588 rtx_equal_p (x, y)
589 rtx x, y;
591 register int i;
592 register int j;
593 register enum rtx_code code;
594 register const char *fmt;
596 if (x == y)
597 return 1;
598 if (x == 0 || y == 0)
599 return 0;
601 code = GET_CODE (x);
602 /* Rtx's of different codes cannot be equal. */
603 if (code != GET_CODE (y))
604 return 0;
606 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
607 (REG:SI x) and (REG:HI x) are NOT equivalent. */
609 if (GET_MODE (x) != GET_MODE (y))
610 return 0;
612 /* REG, LABEL_REF, and SYMBOL_REF can be compared nonrecursively. */
614 if (code == REG)
615 /* Until rtl generation is complete, don't consider a reference to the
616 return register of the current function the same as the return from a
617 called function. This eases the job of function integration. Once the
618 distinction is no longer needed, they can be considered equivalent. */
619 return (REGNO (x) == REGNO (y)
620 && (! rtx_equal_function_value_matters
621 || REG_FUNCTION_VALUE_P (x) == REG_FUNCTION_VALUE_P (y)));
622 else if (code == LABEL_REF)
623 return XEXP (x, 0) == XEXP (y, 0);
624 else if (code == SYMBOL_REF)
625 return XSTR (x, 0) == XSTR (y, 0);
626 else if (code == SCRATCH || code == CONST_DOUBLE)
627 return 0;
629 /* Compare the elements. If any pair of corresponding elements
630 fail to match, return 0 for the whole things. */
632 fmt = GET_RTX_FORMAT (code);
633 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
635 switch (fmt[i])
637 case 'w':
638 if (XWINT (x, i) != XWINT (y, i))
639 return 0;
640 break;
642 case 'n':
643 case 'i':
644 if (XINT (x, i) != XINT (y, i))
645 return 0;
646 break;
648 case 'V':
649 case 'E':
650 /* Two vectors must have the same length. */
651 if (XVECLEN (x, i) != XVECLEN (y, i))
652 return 0;
654 /* And the corresponding elements must match. */
655 for (j = 0; j < XVECLEN (x, i); j++)
656 if (rtx_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)) == 0)
657 return 0;
658 break;
660 case 'e':
661 if (rtx_equal_p (XEXP (x, i), XEXP (y, i)) == 0)
662 return 0;
663 break;
665 case 'S':
666 case 's':
667 if (strcmp (XSTR (x, i), XSTR (y, i)))
668 return 0;
669 break;
671 case 'u':
672 /* These are just backpointers, so they don't matter. */
673 break;
675 case '0':
676 case 't':
677 break;
679 /* It is believed that rtx's at this level will never
680 contain anything but integers and other rtx's,
681 except for within LABEL_REFs and SYMBOL_REFs. */
682 default:
683 abort ();
686 return 1;
689 /* Subroutines of read_rtx. */
691 /* The current line number for the file. */
692 int read_rtx_lineno = 1;
694 /* The filename for aborting with file and line. */
695 const char *read_rtx_filename = "<unknown>";
697 static void
698 fatal_with_file_and_line VPARAMS ((FILE *infile, const char *msg, ...))
700 #ifndef ANSI_PROTOTYPES
701 FILE *infile;
702 const char *msg;
703 #endif
704 va_list ap;
705 char context[64];
706 size_t i;
707 int c;
709 VA_START (ap, msg);
711 #ifndef ANSI_PROTOTYPES
712 infile = va_arg (ap, FILE *);
713 msg = va_arg (ap, const char *);
714 #endif
716 fprintf (stderr, "%s:%d: ", read_rtx_filename, read_rtx_lineno);
717 vfprintf (stderr, msg, ap);
718 putc ('\n', stderr);
720 /* Gather some following context. */
721 for (i = 0; i < sizeof(context)-1; ++i)
723 c = getc (infile);
724 if (c == EOF)
725 break;
726 if (c == '\r' || c == '\n')
727 break;
728 context[i] = c;
730 context[i] = '\0';
732 fprintf (stderr, "%s:%d: following context is `%s'\n",
733 read_rtx_filename, read_rtx_lineno, context);
735 va_end (ap);
736 exit (1);
739 /* Dump code after printing a message. Used when read_rtx finds
740 invalid data. */
742 static void
743 fatal_expected_char (infile, expected_c, actual_c)
744 FILE *infile;
745 int expected_c, actual_c;
747 fatal_with_file_and_line (infile, "expected character `%c', found `%c'",
748 expected_c, actual_c);
751 /* Read chars from INFILE until a non-whitespace char
752 and return that. Comments, both Lisp style and C style,
753 are treated as whitespace.
754 Tools such as genflags use this function. */
757 read_skip_spaces (infile)
758 FILE *infile;
760 register int c;
761 while (1)
763 c = getc (infile);
764 switch (c)
766 case '\n':
767 read_rtx_lineno++;
768 break;
770 case ' ': case '\t': case '\f': case '\r':
771 break;
773 case ';':
775 c = getc (infile);
776 while (c != '\n' && c != EOF);
777 read_rtx_lineno++;
778 break;
780 case '/':
782 register int prevc;
783 c = getc (infile);
784 if (c != '*')
785 fatal_expected_char (infile, '*', c);
787 prevc = 0;
788 while ((c = getc (infile)) && c != EOF)
790 if (c == '\n')
791 read_rtx_lineno++;
792 else if (prevc == '*' && c == '/')
793 break;
794 prevc = c;
797 break;
799 default:
800 return c;
805 /* Read an rtx code name into the buffer STR[].
806 It is terminated by any of the punctuation chars of rtx printed syntax. */
808 static void
809 read_name (str, infile)
810 char *str;
811 FILE *infile;
813 register char *p;
814 register int c;
816 c = read_skip_spaces(infile);
818 p = str;
819 while (1)
821 if (c == ' ' || c == '\n' || c == '\t' || c == '\f')
822 break;
823 if (c == ':' || c == ')' || c == ']' || c == '"' || c == '/'
824 || c == '(' || c == '[')
826 ungetc (c, infile);
827 break;
829 *p++ = c;
830 c = getc (infile);
832 if (p == str)
833 fatal_with_file_and_line (infile, "missing name or number");
834 if (c == '\n')
835 read_rtx_lineno++;
837 *p = 0;
840 /* Provide a version of a function to read a long long if the system does
841 not provide one. */
842 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !defined(HAVE_ATOLL) && !defined(HAVE_ATOQ)
843 HOST_WIDE_INT
844 atoll(p)
845 const char *p;
847 int neg = 0;
848 HOST_WIDE_INT tmp_wide;
850 while (ISSPACE(*p))
851 p++;
852 if (*p == '-')
853 neg = 1, p++;
854 else if (*p == '+')
855 p++;
857 tmp_wide = 0;
858 while (ISDIGIT(*p))
860 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
861 if (new_wide < tmp_wide)
863 /* Return INT_MAX equiv on overflow. */
864 tmp_wide = (~(unsigned HOST_WIDE_INT)0) >> 1;
865 break;
867 tmp_wide = new_wide;
868 p++;
871 if (neg)
872 tmp_wide = -tmp_wide;
873 return tmp_wide;
875 #endif
877 /* Read an rtx in printed representation from INFILE
878 and return an actual rtx in core constructed accordingly.
879 read_rtx is not used in the compiler proper, but rather in
880 the utilities gen*.c that construct C code from machine descriptions. */
883 read_rtx (infile)
884 FILE *infile;
886 register int i, j, list_counter;
887 RTX_CODE tmp_code;
888 register const char *format_ptr;
889 /* tmp_char is a buffer used for reading decimal integers
890 and names of rtx types and machine modes.
891 Therefore, 256 must be enough. */
892 char tmp_char[256];
893 rtx return_rtx;
894 register int c;
895 int tmp_int;
896 HOST_WIDE_INT tmp_wide;
898 /* Linked list structure for making RTXs: */
899 struct rtx_list
901 struct rtx_list *next;
902 rtx value; /* Value of this node. */
905 c = read_skip_spaces (infile); /* Should be open paren. */
906 if (c != '(')
907 fatal_expected_char (infile, '(', c);
909 read_name (tmp_char, infile);
911 tmp_code = UNKNOWN;
913 for (i = 0; i < NUM_RTX_CODE; i++)
914 if (! strcmp (tmp_char, GET_RTX_NAME (i)))
916 tmp_code = (RTX_CODE) i; /* get value for name */
917 break;
920 if (tmp_code == UNKNOWN)
921 fatal_with_file_and_line (infile, "unknown rtx code `%s'", tmp_char);
923 /* (NIL) stands for an expression that isn't there. */
924 if (tmp_code == NIL)
926 /* Discard the closeparen. */
927 while ((c = getc (infile)) && c != ')')
930 return 0;
933 /* If we end up with an insn expression then we free this space below. */
934 return_rtx = rtx_alloc (tmp_code);
935 format_ptr = GET_RTX_FORMAT (GET_CODE (return_rtx));
937 /* If what follows is `: mode ', read it and
938 store the mode in the rtx. */
940 i = read_skip_spaces (infile);
941 if (i == ':')
943 read_name (tmp_char, infile);
944 for (j = 0; j < NUM_MACHINE_MODES; j++)
945 if (! strcmp (GET_MODE_NAME (j), tmp_char))
946 break;
948 if (j == MAX_MACHINE_MODE)
949 fatal_with_file_and_line (infile, "unknown mode `%s'", tmp_char);
951 PUT_MODE (return_rtx, (enum machine_mode) j);
953 else
954 ungetc (i, infile);
956 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (return_rtx)); i++)
957 switch (*format_ptr++)
959 /* 0 means a field for internal use only.
960 Don't expect it to be present in the input. */
961 case '0':
962 break;
964 case 'e':
965 case 'u':
966 XEXP (return_rtx, i) = read_rtx (infile);
967 break;
969 case 'V':
970 /* 'V' is an optional vector: if a closeparen follows,
971 just store NULL for this element. */
972 c = read_skip_spaces (infile);
973 ungetc (c, infile);
974 if (c == ')')
976 XVEC (return_rtx, i) = 0;
977 break;
979 /* Now process the vector. */
981 case 'E':
983 register struct rtx_list *next_rtx, *rtx_list_link;
984 struct rtx_list *list_rtx = NULL;
986 c = read_skip_spaces (infile);
987 if (c != '[')
988 fatal_expected_char (infile, '[', c);
990 /* add expressions to a list, while keeping a count */
991 next_rtx = NULL;
992 list_counter = 0;
993 while ((c = read_skip_spaces (infile)) && c != ']')
995 ungetc (c, infile);
996 list_counter++;
997 rtx_list_link = (struct rtx_list *)
998 alloca (sizeof (struct rtx_list));
999 rtx_list_link->value = read_rtx (infile);
1000 if (next_rtx == 0)
1001 list_rtx = rtx_list_link;
1002 else
1003 next_rtx->next = rtx_list_link;
1004 next_rtx = rtx_list_link;
1005 rtx_list_link->next = 0;
1007 /* get vector length and allocate it */
1008 XVEC (return_rtx, i) = (list_counter
1009 ? rtvec_alloc (list_counter) : NULL_RTVEC);
1010 if (list_counter > 0)
1012 next_rtx = list_rtx;
1013 for (j = 0; j < list_counter; j++,
1014 next_rtx = next_rtx->next)
1015 XVECEXP (return_rtx, i, j) = next_rtx->value;
1017 /* close bracket gotten */
1019 break;
1021 case 'S':
1022 /* 'S' is an optional string: if a closeparen follows,
1023 just store NULL for this element. */
1024 c = read_skip_spaces (infile);
1025 ungetc (c, infile);
1026 if (c == ')')
1028 XSTR (return_rtx, i) = 0;
1029 break;
1032 case 's':
1034 int saw_paren = 0;
1035 register char *stringbuf;
1037 c = read_skip_spaces (infile);
1038 if (c == '(')
1040 saw_paren = 1;
1041 c = read_skip_spaces (infile);
1043 if (c != '"')
1044 fatal_expected_char (infile, '"', c);
1046 while (1)
1048 c = getc (infile); /* Read the string */
1049 if (c == '\n')
1050 read_rtx_lineno++;
1051 if (c == '\\')
1053 c = getc (infile); /* Read the string */
1054 /* \; makes stuff for a C string constant containing
1055 newline and tab. */
1056 if (c == ';')
1058 obstack_grow (rtl_obstack, "\\n\\t", 4);
1059 continue;
1061 if (c == '\n')
1062 read_rtx_lineno++;
1064 else if (c == '"')
1065 break;
1067 obstack_1grow (rtl_obstack, c);
1070 obstack_1grow (rtl_obstack, 0);
1071 stringbuf = (char *) obstack_finish (rtl_obstack);
1073 if (saw_paren)
1075 c = read_skip_spaces (infile);
1076 if (c != ')')
1077 fatal_expected_char (infile, ')', c);
1079 XSTR (return_rtx, i) = stringbuf;
1081 break;
1083 case 'w':
1084 read_name (tmp_char, infile);
1085 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1086 tmp_wide = atoi (tmp_char);
1087 #else
1088 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1089 tmp_wide = atol (tmp_char);
1090 #else
1091 /* Prefer atoll over atoq, since the former is in the ISO C9X draft.
1092 But prefer not to use our hand-rolled function above either. */
1093 #if defined(HAVE_ATOLL) || !defined(HAVE_ATOQ)
1094 tmp_wide = atoll (tmp_char);
1095 #else
1096 tmp_wide = atoq (tmp_char);
1097 #endif
1098 #endif
1099 #endif
1100 XWINT (return_rtx, i) = tmp_wide;
1101 break;
1103 case 'i':
1104 case 'n':
1105 read_name (tmp_char, infile);
1106 tmp_int = atoi (tmp_char);
1107 XINT (return_rtx, i) = tmp_int;
1108 break;
1110 default:
1111 fprintf (stderr,
1112 "switch format wrong in rtl.read_rtx(). format was: %c.\n",
1113 format_ptr[-1]);
1114 fprintf (stderr, "\tfile position: %ld\n", ftell (infile));
1115 abort ();
1118 c = read_skip_spaces (infile);
1119 if (c != ')')
1120 fatal_expected_char (infile, ')', c);
1122 return return_rtx;
1125 #if defined ENABLE_RTL_CHECKING && (GCC_VERSION >= 2007)
1126 void
1127 rtl_check_failed_bounds (r, n, file, line, func)
1128 rtx r;
1129 int n;
1130 const char *file;
1131 int line;
1132 const char *func;
1134 error ("RTL check: access of elt %d of `%s' with last elt %d",
1135 n, GET_RTX_NAME (GET_CODE (r)), GET_RTX_LENGTH (GET_CODE (r))-1);
1136 fancy_abort (file, line, func);
1139 void
1140 rtl_check_failed_type1 (r, n, c1, file, line, func)
1141 rtx r;
1142 int n;
1143 int c1;
1144 const char *file;
1145 int line;
1146 const char *func;
1148 error ("RTL check: expected elt %d type '%c', have '%c' (rtx %s)",
1149 n, c1, GET_RTX_FORMAT (GET_CODE (r))[n], GET_RTX_NAME (GET_CODE (r)));
1150 fancy_abort (file, line, func);
1153 void
1154 rtl_check_failed_type2 (r, n, c1, c2, file, line, func)
1155 rtx r;
1156 int n;
1157 int c1;
1158 int c2;
1159 const char *file;
1160 int line;
1161 const char *func;
1163 error ("RTL check: expected elt %d type '%c' or '%c', have '%c' (rtx %s)",
1164 n, c1, c2,
1165 GET_RTX_FORMAT (GET_CODE (r))[n], GET_RTX_NAME (GET_CODE(r)));
1166 fancy_abort (file, line, func);
1169 void
1170 rtl_check_failed_code1 (r, code, file, line, func)
1171 rtx r;
1172 enum rtx_code code;
1173 const char *file;
1174 int line;
1175 const char *func;
1177 error ("RTL check: expected code `%s', have `%s'",
1178 GET_RTX_NAME (code), GET_RTX_NAME (GET_CODE (r)));
1179 fancy_abort (file, line, func);
1182 void
1183 rtl_check_failed_code2 (r, code1, code2, file, line, func)
1184 rtx r;
1185 enum rtx_code code1, code2;
1186 const char *file;
1187 int line;
1188 const char *func;
1190 error ("RTL check: expected code `%s' or `%s', have `%s'",
1191 GET_RTX_NAME (code1), GET_RTX_NAME (code2),
1192 GET_RTX_NAME (GET_CODE (r)));
1193 fancy_abort (file, line, func);
1196 /* XXX Maybe print the vector? */
1197 void
1198 rtvec_check_failed_bounds (r, n, file, line, func)
1199 rtvec r;
1200 int n;
1201 const char *file;
1202 int line;
1203 const char *func;
1205 error ("RTL check: access of elt %d of vector with last elt %d",
1206 n, GET_NUM_ELEM (r)-1);
1207 fancy_abort (file, line, func);
1209 #endif /* ENABLE_RTL_CHECKING */
1211 /* These are utility functions used by fatal-error functions all over the
1212 code. rtl.c happens to be linked by all the programs that need them,
1213 so these are here. In the future we want to break out all error handling
1214 to its own module. */
1216 /* Given a partial pathname as input, return another pathname that
1217 shares no directory elements with the pathname of __FILE__. This
1218 is used by fancy_abort() to print `Internal compiler error in expr.c'
1219 instead of `Internal compiler error in ../../egcs/gcc/expr.c'. */
1220 static const char *
1221 trim_filename (name)
1222 const char *name;
1224 static const char this_file[] = __FILE__;
1225 const char *p = name, *q = this_file;
1227 while (*p == *q && *p != 0 && *q != 0) p++, q++;
1228 while (p > name && p[-1] != DIR_SEPARATOR
1229 #ifdef DIR_SEPARATOR_2
1230 && p[-1] != DIR_SEPARATOR_2
1231 #endif
1233 p--;
1235 return p;
1238 /* Report an internal compiler error in a friendly manner and without
1239 dumping core. */
1241 void
1242 fancy_abort (file, line, function)
1243 const char *file;
1244 int line;
1245 const char *function;
1247 if (function == NULL)
1248 function = "?";
1249 fatal (
1250 "Internal compiler error in `%s', at %s:%d\n\
1251 Please submit a full bug report.\n\
1252 See %s for instructions.",
1253 function, trim_filename (file), line, GCCBUGURL);