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