* config/i386/i386.md (mmx_pinsrw): Output operands in correct
[official-gcc.git] / gcc / rtl.c
blobbb986e998b9cf8917d4398d4538f104692c12fd7
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
36 /* Calculate the format for CONST_DOUBLE. This depends on the relative
37 widths of HOST_WIDE_INT and REAL_VALUE_TYPE.
39 We need to go out to e0wwwww, since REAL_ARITHMETIC assumes 16-bits
40 per element in REAL_VALUE_TYPE.
42 This is duplicated in gengenrtl.c.
44 A number of places assume that there are always at least two 'w'
45 slots in a CONST_DOUBLE, so we provide them even if one would suffice. */
47 #ifdef REAL_ARITHMETIC
48 # if MAX_LONG_DOUBLE_TYPE_SIZE == 96
49 # define REAL_WIDTH \
50 (11*8 + HOST_BITS_PER_WIDE_INT)/HOST_BITS_PER_WIDE_INT
51 # else
52 # if MAX_LONG_DOUBLE_TYPE_SIZE == 128
53 # define REAL_WIDTH \
54 (19*8 + HOST_BITS_PER_WIDE_INT)/HOST_BITS_PER_WIDE_INT
55 # else
56 # if HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT
57 # define REAL_WIDTH \
58 (7*8 + HOST_BITS_PER_WIDE_INT)/HOST_BITS_PER_WIDE_INT
59 # endif
60 # endif
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 # else
68 # if HOST_BITS_PER_WIDE_INT*3 >= MAX_LONG_DOUBLE_TYPE_SIZE
69 # define REAL_WIDTH 3
70 # else
71 # if HOST_BITS_PER_WIDE_INT*4 >= MAX_LONG_DOUBLE_TYPE_SIZE
72 # define REAL_WIDTH 4
73 # endif
74 # endif
75 # endif
76 #endif /* REAL_WIDTH */
78 #if REAL_WIDTH == 1
79 # define CONST_DOUBLE_FORMAT "e0ww"
80 #else
81 # if REAL_WIDTH == 2
82 # define CONST_DOUBLE_FORMAT "e0ww"
83 # else
84 # if REAL_WIDTH == 3
85 # define CONST_DOUBLE_FORMAT "e0www"
86 # else
87 # if REAL_WIDTH == 4
88 # define CONST_DOUBLE_FORMAT "e0wwww"
89 # else
90 # if REAL_WIDTH == 5
91 # define CONST_DOUBLE_FORMAT "e0wwwww"
92 # else
93 # define CONST_DOUBLE_FORMAT /* nothing - will cause syntax error */
94 # endif
95 # endif
96 # endif
97 # endif
98 #endif
100 /* Indexed by rtx code, gives number of operands for an rtx with that code.
101 Does NOT include rtx header data (code and links). */
103 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) sizeof FORMAT - 1 ,
105 const int rtx_length[NUM_RTX_CODE + 1] = {
106 #include "rtl.def"
109 #undef DEF_RTL_EXPR
111 /* Indexed by rtx code, gives the name of that kind of rtx, as a C string. */
113 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
115 const char * const rtx_name[] = {
116 #include "rtl.def" /* rtl expressions are documented here */
119 #undef DEF_RTL_EXPR
121 /* Indexed by machine mode, gives the name of that machine mode.
122 This name does not include the letters "mode". */
124 #define DEF_MACHMODE(SYM, NAME, CLASS, BITSIZE, SIZE, UNIT, WIDER) NAME,
126 const char * const mode_name[(int) MAX_MACHINE_MODE + 1] = {
127 #include "machmode.def"
128 /* Add an extra field to avoid a core dump if someone tries to convert
129 MAX_MACHINE_MODE to a string. */
133 #undef DEF_MACHMODE
135 /* Indexed by machine mode, gives the class mode for GET_MODE_CLASS. */
137 #define DEF_MACHMODE(SYM, NAME, CLASS, BITSIZE, SIZE, UNIT, WIDER) CLASS,
139 const enum mode_class mode_class[(int) MAX_MACHINE_MODE] = {
140 #include "machmode.def"
143 #undef DEF_MACHMODE
145 /* Indexed by machine mode, gives the length of the mode, in bits.
146 GET_MODE_BITSIZE uses this. */
148 #define DEF_MACHMODE(SYM, NAME, CLASS, BITSIZE, SIZE, UNIT, WIDER) BITSIZE,
150 const unsigned int mode_bitsize[(int) MAX_MACHINE_MODE] = {
151 #include "machmode.def"
154 #undef DEF_MACHMODE
156 /* Indexed by machine mode, gives the length of the mode, in bytes.
157 GET_MODE_SIZE uses this. */
159 #define DEF_MACHMODE(SYM, NAME, CLASS, BITSIZE, SIZE, UNIT, WIDER) SIZE,
161 const unsigned int mode_size[(int) MAX_MACHINE_MODE] = {
162 #include "machmode.def"
165 #undef DEF_MACHMODE
167 /* Indexed by machine mode, gives the length of the mode's subunit.
168 GET_MODE_UNIT_SIZE uses this. */
170 #define DEF_MACHMODE(SYM, NAME, CLASS, BITSIZE, SIZE, UNIT, WIDER) UNIT,
172 const unsigned int mode_unit_size[(int) MAX_MACHINE_MODE] = {
173 #include "machmode.def" /* machine modes are documented here */
176 #undef DEF_MACHMODE
178 /* Indexed by machine mode, gives next wider natural mode
179 (QI -> HI -> SI -> DI, etc.) Widening multiply instructions
180 use this. */
182 #define DEF_MACHMODE(SYM, NAME, CLASS, BITSIZE, SIZE, UNIT, WIDER) \
183 (unsigned char) WIDER,
185 const unsigned char mode_wider_mode[(int) MAX_MACHINE_MODE] = {
186 #include "machmode.def" /* machine modes are documented here */
189 #undef DEF_MACHMODE
191 #define DEF_MACHMODE(SYM, NAME, CLASS, BITSIZE, SIZE, UNIT, WIDER) \
192 ((BITSIZE) >= HOST_BITS_PER_WIDE_INT) ? ~(unsigned HOST_WIDE_INT)0 : ((unsigned HOST_WIDE_INT) 1 << (BITSIZE)) - 1,
194 /* Indexed by machine mode, gives mask of significant bits in mode. */
196 const unsigned HOST_WIDE_INT mode_mask_array[(int) MAX_MACHINE_MODE] = {
197 #include "machmode.def"
200 /* Indexed by mode class, gives the narrowest mode for each class.
201 The Q modes are always of width 1 (2 for complex) - it is impossible
202 for any mode to be narrower.
204 Note that we use QImode instead of BImode for MODE_INT, since
205 otherwise the middle end will try to use it for bitfields in
206 structures and the like, which we do not want. Only the target
207 md file should generate BImode widgets. */
209 const enum machine_mode class_narrowest_mode[(int) MAX_MODE_CLASS] = {
210 /* MODE_RANDOM */ VOIDmode,
211 /* MODE_INT */ QImode,
212 /* MODE_FLOAT */ QFmode,
213 /* MODE_PARTIAL_INT */ PQImode,
214 /* MODE_CC */ CCmode,
215 /* MODE_COMPLEX_INT */ CQImode,
216 /* MODE_COMPLEX_FLOAT */ QCmode,
217 /* MODE_VECTOR_INT */ V2QImode,
218 /* MODE_VECTOR_FLOAT */ V2SFmode
222 /* Indexed by rtx code, gives a sequence of operand-types for
223 rtx's of that code. The sequence is a C string in which
224 each character describes one operand. */
226 const char * const rtx_format[] = {
227 /* "*" undefined.
228 can cause a warning message
229 "0" field is unused (or used in a phase-dependent manner)
230 prints nothing
231 "i" an integer
232 prints the integer
233 "n" like "i", but prints entries from `note_insn_name'
234 "w" an integer of width HOST_BITS_PER_WIDE_INT
235 prints the integer
236 "s" a pointer to a string
237 prints the string
238 "S" like "s", but optional:
239 the containing rtx may end before this operand
240 "e" a pointer to an rtl expression
241 prints the expression
242 "E" a pointer to a vector that points to a number of rtl expressions
243 prints a list of the rtl expressions
244 "V" like "E", but optional:
245 the containing rtx may end before this operand
246 "u" a pointer to another insn
247 prints the uid of the insn.
248 "b" is a pointer to a bitmap header.
249 "t" is a tree pointer. */
251 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
252 #include "rtl.def" /* rtl expressions are defined here */
253 #undef DEF_RTL_EXPR
256 /* Indexed by rtx code, gives a character representing the "class" of
257 that rtx code. See rtl.def for documentation on the defined classes. */
259 const char rtx_class[] = {
260 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) CLASS,
261 #include "rtl.def" /* rtl expressions are defined here */
262 #undef DEF_RTL_EXPR
265 /* Names for kinds of NOTEs and REG_NOTEs. */
267 const char * const note_insn_name[NOTE_INSN_MAX - NOTE_INSN_BIAS] =
269 "", "NOTE_INSN_DELETED",
270 "NOTE_INSN_BLOCK_BEG", "NOTE_INSN_BLOCK_END",
271 "NOTE_INSN_LOOP_BEG", "NOTE_INSN_LOOP_END",
272 "NOTE_INSN_LOOP_CONT", "NOTE_INSN_LOOP_VTOP",
273 "NOTE_INSN_FUNCTION_END", "NOTE_INSN_SETJMP",
274 "NOTE_INSN_PROLOGUE_END", "NOTE_INSN_EPILOGUE_BEG",
275 "NOTE_INSN_DELETED_LABEL", "NOTE_INSN_FUNCTION_BEG",
276 "NOTE_INSN_EH_REGION_BEG", "NOTE_INSN_EH_REGION_END",
277 "NOTE_INSN_REPEATED_LINE_NUMBER", "NOTE_INSN_RANGE_BEG",
278 "NOTE_INSN_RANGE_END", "NOTE_INSN_LIVE",
279 "NOTE_INSN_BASIC_BLOCK", "NOTE_INSN_EXPECTED_VALUE"
282 const char * const reg_note_name[] =
284 "", "REG_DEAD", "REG_INC", "REG_EQUIV", "REG_EQUAL",
285 "REG_WAS_0", "REG_RETVAL", "REG_LIBCALL", "REG_NONNEG",
286 "REG_NO_CONFLICT", "REG_UNUSED", "REG_CC_SETTER", "REG_CC_USER",
287 "REG_LABEL", "REG_DEP_ANTI", "REG_DEP_OUTPUT", "REG_BR_PROB",
288 "REG_EXEC_COUNT", "REG_NOALIAS", "REG_SAVE_AREA", "REG_BR_PRED",
289 "REG_FRAME_RELATED_EXPR", "REG_EH_CONTEXT", "REG_EH_REGION",
290 "REG_EH_RETHROW", "REG_SAVE_NOTE", "REG_MAYBE_DEAD", "REG_NORETURN"
293 static void fatal_with_file_and_line PARAMS ((FILE *, const char *, ...))
294 ATTRIBUTE_PRINTF_2 ATTRIBUTE_NORETURN;
295 static void fatal_expected_char PARAMS ((FILE *, int, int)) ATTRIBUTE_NORETURN;
296 static void read_name PARAMS ((char *, FILE *));
299 /* Allocate an rtx vector of N elements.
300 Store the length, and initialize all elements to zero. */
302 rtvec
303 rtvec_alloc (n)
304 int n;
306 rtvec rt;
308 rt = ggc_alloc_rtvec (n);
309 /* clear out the vector */
310 memset (&rt->elem[0], 0, n * sizeof (rtx));
312 PUT_NUM_ELEM (rt, n);
313 return rt;
316 /* Allocate an rtx of code CODE. The CODE is stored in the rtx;
317 all the rest is initialized to zero. */
320 rtx_alloc (code)
321 RTX_CODE code;
323 rtx rt;
324 int n = GET_RTX_LENGTH (code);
326 rt = ggc_alloc_rtx (n);
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;
338 /* Create a new copy of an rtx.
339 Recursively copies the operands of the rtx,
340 except for those few rtx codes that are sharable. */
343 copy_rtx (orig)
344 register rtx orig;
346 register rtx copy;
347 register int i, j;
348 register RTX_CODE code;
349 register const char *format_ptr;
351 code = GET_CODE (orig);
353 switch (code)
355 case REG:
356 case QUEUED:
357 case CONST_INT:
358 case CONST_DOUBLE:
359 case SYMBOL_REF:
360 case CODE_LABEL:
361 case PC:
362 case CC0:
363 case SCRATCH:
364 /* SCRATCH must be shared because they represent distinct values. */
365 case ADDRESSOF:
366 return orig;
368 case CONST:
369 /* CONST can be shared if it contains a SYMBOL_REF. If it contains
370 a LABEL_REF, it isn't sharable. */
371 if (GET_CODE (XEXP (orig, 0)) == PLUS
372 && GET_CODE (XEXP (XEXP (orig, 0), 0)) == SYMBOL_REF
373 && GET_CODE (XEXP (XEXP (orig, 0), 1)) == CONST_INT)
374 return orig;
375 break;
377 /* A MEM with a constant address is not sharable. The problem is that
378 the constant address may need to be reloaded. If the mem is shared,
379 then reloading one copy of this mem will cause all copies to appear
380 to have been reloaded. */
382 default:
383 break;
386 copy = rtx_alloc (code);
388 /* Copy the various flags, and other information. We assume that
389 all fields need copying, and then clear the fields that should
390 not be copied. That is the sensible default behavior, and forces
391 us to explicitly document why we are *not* copying a flag. */
392 memcpy (copy, orig, sizeof (struct rtx_def) - sizeof (rtunion));
394 /* We do not copy the USED flag, which is used as a mark bit during
395 walks over the RTL. */
396 copy->used = 0;
398 /* We do not copy FRAME_RELATED for INSNs. */
399 if (GET_RTX_CLASS (code) == 'i')
400 copy->frame_related = 0;
401 copy->jump = orig->jump;
402 copy->call = orig->call;
404 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
406 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
408 copy->fld[i] = orig->fld[i];
409 switch (*format_ptr++)
411 case 'e':
412 if (XEXP (orig, i) != NULL)
413 XEXP (copy, i) = copy_rtx (XEXP (orig, i));
414 break;
416 case 'E':
417 case 'V':
418 if (XVEC (orig, i) != NULL)
420 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
421 for (j = 0; j < XVECLEN (copy, i); j++)
422 XVECEXP (copy, i, j) = copy_rtx (XVECEXP (orig, i, j));
424 break;
426 case 't':
427 case 'w':
428 case 'i':
429 case 's':
430 case 'S':
431 case 'u':
432 case '0':
433 /* These are left unchanged. */
434 break;
436 default:
437 abort ();
440 return copy;
443 /* Similar to `copy_rtx' except that if MAY_SHARE is present, it is
444 placed in the result directly, rather than being copied. */
447 copy_most_rtx (orig, may_share)
448 register rtx orig;
449 register rtx may_share;
451 register rtx copy;
452 register int i, j;
453 register RTX_CODE code;
454 register const char *format_ptr;
456 if (orig == may_share)
457 return orig;
459 code = GET_CODE (orig);
461 switch (code)
463 case REG:
464 case QUEUED:
465 case CONST_INT:
466 case CONST_DOUBLE:
467 case SYMBOL_REF:
468 case CODE_LABEL:
469 case PC:
470 case CC0:
471 return orig;
472 default:
473 break;
476 copy = rtx_alloc (code);
477 PUT_MODE (copy, GET_MODE (orig));
478 copy->in_struct = orig->in_struct;
479 copy->volatil = orig->volatil;
480 copy->unchanging = orig->unchanging;
481 copy->integrated = orig->integrated;
483 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
485 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
487 switch (*format_ptr++)
489 case 'e':
490 XEXP (copy, i) = XEXP (orig, i);
491 if (XEXP (orig, i) != NULL && XEXP (orig, i) != may_share)
492 XEXP (copy, i) = copy_most_rtx (XEXP (orig, i), may_share);
493 break;
495 case 'u':
496 XEXP (copy, i) = XEXP (orig, i);
497 break;
499 case 'E':
500 case 'V':
501 XVEC (copy, i) = XVEC (orig, i);
502 if (XVEC (orig, i) != NULL)
504 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
505 for (j = 0; j < XVECLEN (copy, i); j++)
506 XVECEXP (copy, i, j)
507 = copy_most_rtx (XVECEXP (orig, i, j), may_share);
509 break;
511 case 'w':
512 XWINT (copy, i) = XWINT (orig, i);
513 break;
515 case 'n':
516 case 'i':
517 XINT (copy, i) = XINT (orig, i);
518 break;
520 case 't':
521 XTREE (copy, i) = XTREE (orig, i);
522 break;
524 case 's':
525 case 'S':
526 XSTR (copy, i) = XSTR (orig, i);
527 break;
529 case '0':
530 /* Copy this through the wide int field; that's safest. */
531 X0WINT (copy, i) = X0WINT (orig, i);
532 break;
534 default:
535 abort ();
538 return copy;
541 /* Create a new copy of an rtx. Only copy just one level. */
543 shallow_copy_rtx (orig)
544 rtx orig;
546 register int i;
547 register RTX_CODE code = GET_CODE (orig);
548 register rtx copy = rtx_alloc (code);
550 PUT_MODE (copy, GET_MODE (orig));
551 copy->in_struct = orig->in_struct;
552 copy->volatil = orig->volatil;
553 copy->unchanging = orig->unchanging;
554 copy->integrated = orig->integrated;
556 for (i = 0; i < GET_RTX_LENGTH (code); i++)
557 copy->fld[i] = orig->fld[i];
559 return copy;
562 /* This is 1 until after the rtl generation pass. */
563 int rtx_equal_function_value_matters;
565 /* Nonzero when we are generating CONCATs. */
566 int generating_concat_p;
568 /* Return 1 if X and Y are identical-looking rtx's.
569 This is the Lisp function EQUAL for rtx arguments. */
572 rtx_equal_p (x, y)
573 rtx x, y;
575 register int i;
576 register int j;
577 register enum rtx_code code;
578 register const char *fmt;
580 if (x == y)
581 return 1;
582 if (x == 0 || y == 0)
583 return 0;
585 code = GET_CODE (x);
586 /* Rtx's of different codes cannot be equal. */
587 if (code != GET_CODE (y))
588 return 0;
590 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
591 (REG:SI x) and (REG:HI x) are NOT equivalent. */
593 if (GET_MODE (x) != GET_MODE (y))
594 return 0;
596 /* Some RTL can be compared nonrecursively. */
597 switch (code)
599 case REG:
600 /* Until rtl generation is complete, don't consider a reference to the
601 return register of the current function the same as the return from a
602 called function. This eases the job of function integration. Once the
603 distinction is no longer needed, they can be considered equivalent. */
604 return (REGNO (x) == REGNO (y)
605 && (! rtx_equal_function_value_matters
606 || REG_FUNCTION_VALUE_P (x) == REG_FUNCTION_VALUE_P (y)));
608 case LABEL_REF:
609 return XEXP (x, 0) == XEXP (y, 0);
611 case SYMBOL_REF:
612 return XSTR (x, 0) == XSTR (y, 0);
614 case SCRATCH:
615 case CONST_DOUBLE:
616 case CONST_INT:
617 return 0;
619 default:
620 break;
623 /* Compare the elements. If any pair of corresponding elements
624 fail to match, return 0 for the whole things. */
626 fmt = GET_RTX_FORMAT (code);
627 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
629 switch (fmt[i])
631 case 'w':
632 if (XWINT (x, i) != XWINT (y, i))
633 return 0;
634 break;
636 case 'n':
637 case 'i':
638 if (XINT (x, i) != XINT (y, i))
639 return 0;
640 break;
642 case 'V':
643 case 'E':
644 /* Two vectors must have the same length. */
645 if (XVECLEN (x, i) != XVECLEN (y, i))
646 return 0;
648 /* And the corresponding elements must match. */
649 for (j = 0; j < XVECLEN (x, i); j++)
650 if (rtx_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)) == 0)
651 return 0;
652 break;
654 case 'e':
655 if (rtx_equal_p (XEXP (x, i), XEXP (y, i)) == 0)
656 return 0;
657 break;
659 case 'S':
660 case 's':
661 if (strcmp (XSTR (x, i), XSTR (y, i)))
662 return 0;
663 break;
665 case 'u':
666 /* These are just backpointers, so they don't matter. */
667 break;
669 case '0':
670 case 't':
671 break;
673 /* It is believed that rtx's at this level will never
674 contain anything but integers and other rtx's,
675 except for within LABEL_REFs and SYMBOL_REFs. */
676 default:
677 abort ();
680 return 1;
683 /* Subroutines of read_rtx. */
685 /* The current line number for the file. */
686 int read_rtx_lineno = 1;
688 /* The filename for aborting with file and line. */
689 const char *read_rtx_filename = "<unknown>";
691 static void
692 fatal_with_file_and_line VPARAMS ((FILE *infile, const char *msg, ...))
694 #ifndef ANSI_PROTOTYPES
695 FILE *infile;
696 const char *msg;
697 #endif
698 va_list ap;
699 char context[64];
700 size_t i;
701 int c;
703 VA_START (ap, msg);
705 #ifndef ANSI_PROTOTYPES
706 infile = va_arg (ap, FILE *);
707 msg = va_arg (ap, const char *);
708 #endif
710 fprintf (stderr, "%s:%d: ", read_rtx_filename, read_rtx_lineno);
711 vfprintf (stderr, msg, ap);
712 putc ('\n', stderr);
714 /* Gather some following context. */
715 for (i = 0; i < sizeof(context)-1; ++i)
717 c = getc (infile);
718 if (c == EOF)
719 break;
720 if (c == '\r' || c == '\n')
721 break;
722 context[i] = c;
724 context[i] = '\0';
726 fprintf (stderr, "%s:%d: following context is `%s'\n",
727 read_rtx_filename, read_rtx_lineno, context);
729 va_end (ap);
730 exit (1);
733 /* Dump code after printing a message. Used when read_rtx finds
734 invalid data. */
736 static void
737 fatal_expected_char (infile, expected_c, actual_c)
738 FILE *infile;
739 int expected_c, actual_c;
741 fatal_with_file_and_line (infile, "expected character `%c', found `%c'",
742 expected_c, actual_c);
745 /* Read chars from INFILE until a non-whitespace char
746 and return that. Comments, both Lisp style and C style,
747 are treated as whitespace.
748 Tools such as genflags use this function. */
751 read_skip_spaces (infile)
752 FILE *infile;
754 register int c;
755 while (1)
757 c = getc (infile);
758 switch (c)
760 case '\n':
761 read_rtx_lineno++;
762 break;
764 case ' ': case '\t': case '\f': case '\r':
765 break;
767 case ';':
769 c = getc (infile);
770 while (c != '\n' && c != EOF);
771 read_rtx_lineno++;
772 break;
774 case '/':
776 register int prevc;
777 c = getc (infile);
778 if (c != '*')
779 fatal_expected_char (infile, '*', c);
781 prevc = 0;
782 while ((c = getc (infile)) && c != EOF)
784 if (c == '\n')
785 read_rtx_lineno++;
786 else if (prevc == '*' && c == '/')
787 break;
788 prevc = c;
791 break;
793 default:
794 return c;
799 /* Read an rtx code name into the buffer STR[].
800 It is terminated by any of the punctuation chars of rtx printed syntax. */
802 static void
803 read_name (str, infile)
804 char *str;
805 FILE *infile;
807 register char *p;
808 register int c;
810 c = read_skip_spaces(infile);
812 p = str;
813 while (1)
815 if (c == ' ' || c == '\n' || c == '\t' || c == '\f')
816 break;
817 if (c == ':' || c == ')' || c == ']' || c == '"' || c == '/'
818 || c == '(' || c == '[')
820 ungetc (c, infile);
821 break;
823 *p++ = c;
824 c = getc (infile);
826 if (p == str)
827 fatal_with_file_and_line (infile, "missing name or number");
828 if (c == '\n')
829 read_rtx_lineno++;
831 *p = 0;
834 /* Provide a version of a function to read a long long if the system does
835 not provide one. */
836 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !defined(HAVE_ATOLL) && !defined(HAVE_ATOQ)
837 HOST_WIDE_INT
838 atoll(p)
839 const char *p;
841 int neg = 0;
842 HOST_WIDE_INT tmp_wide;
844 while (ISSPACE(*p))
845 p++;
846 if (*p == '-')
847 neg = 1, p++;
848 else if (*p == '+')
849 p++;
851 tmp_wide = 0;
852 while (ISDIGIT(*p))
854 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
855 if (new_wide < tmp_wide)
857 /* Return INT_MAX equiv on overflow. */
858 tmp_wide = (~(unsigned HOST_WIDE_INT)0) >> 1;
859 break;
861 tmp_wide = new_wide;
862 p++;
865 if (neg)
866 tmp_wide = -tmp_wide;
867 return tmp_wide;
869 #endif
871 /* Read an rtx in printed representation from INFILE
872 and return an actual rtx in core constructed accordingly.
873 read_rtx is not used in the compiler proper, but rather in
874 the utilities gen*.c that construct C code from machine descriptions. */
877 read_rtx (infile)
878 FILE *infile;
880 register int i, j, list_counter;
881 RTX_CODE tmp_code;
882 register const char *format_ptr;
883 /* tmp_char is a buffer used for reading decimal integers
884 and names of rtx types and machine modes.
885 Therefore, 256 must be enough. */
886 char tmp_char[256];
887 rtx return_rtx;
888 register int c;
889 int tmp_int;
890 HOST_WIDE_INT tmp_wide;
892 /* Obstack used for allocating RTL objects. */
893 static struct obstack rtl_obstack;
894 static int initialized;
896 /* Linked list structure for making RTXs: */
897 struct rtx_list
899 struct rtx_list *next;
900 rtx value; /* Value of this node. */
903 if (!initialized) {
904 _obstack_begin (&rtl_obstack,0, 0,
905 (void *(*) PARAMS ((long))) xmalloc,
906 (void (*) PARAMS ((void *))) free);
907 initialized = 1;
910 c = read_skip_spaces (infile); /* Should be open paren. */
911 if (c != '(')
912 fatal_expected_char (infile, '(', c);
914 read_name (tmp_char, infile);
916 tmp_code = UNKNOWN;
918 for (i = 0; i < NUM_RTX_CODE; i++)
919 if (! strcmp (tmp_char, GET_RTX_NAME (i)))
921 tmp_code = (RTX_CODE) i; /* get value for name */
922 break;
925 if (tmp_code == UNKNOWN)
926 fatal_with_file_and_line (infile, "unknown rtx code `%s'", tmp_char);
928 /* (NIL) stands for an expression that isn't there. */
929 if (tmp_code == NIL)
931 /* Discard the closeparen. */
932 while ((c = getc (infile)) && c != ')')
935 return 0;
938 /* If we end up with an insn expression then we free this space below. */
939 return_rtx = rtx_alloc (tmp_code);
940 format_ptr = GET_RTX_FORMAT (GET_CODE (return_rtx));
942 /* If what follows is `: mode ', read it and
943 store the mode in the rtx. */
945 i = read_skip_spaces (infile);
946 if (i == ':')
948 read_name (tmp_char, infile);
949 for (j = 0; j < NUM_MACHINE_MODES; j++)
950 if (! strcmp (GET_MODE_NAME (j), tmp_char))
951 break;
953 if (j == MAX_MACHINE_MODE)
954 fatal_with_file_and_line (infile, "unknown mode `%s'", tmp_char);
956 PUT_MODE (return_rtx, (enum machine_mode) j);
958 else
959 ungetc (i, infile);
961 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (return_rtx)); i++)
962 switch (*format_ptr++)
964 /* 0 means a field for internal use only.
965 Don't expect it to be present in the input. */
966 case '0':
967 break;
969 case 'e':
970 case 'u':
971 XEXP (return_rtx, i) = read_rtx (infile);
972 break;
974 case 'V':
975 /* 'V' is an optional vector: if a closeparen follows,
976 just store NULL for this element. */
977 c = read_skip_spaces (infile);
978 ungetc (c, infile);
979 if (c == ')')
981 XVEC (return_rtx, i) = 0;
982 break;
984 /* Now process the vector. */
986 case 'E':
988 register struct rtx_list *next_rtx, *rtx_list_link;
989 struct rtx_list *list_rtx = NULL;
991 c = read_skip_spaces (infile);
992 if (c != '[')
993 fatal_expected_char (infile, '[', c);
995 /* add expressions to a list, while keeping a count */
996 next_rtx = NULL;
997 list_counter = 0;
998 while ((c = read_skip_spaces (infile)) && c != ']')
1000 ungetc (c, infile);
1001 list_counter++;
1002 rtx_list_link = (struct rtx_list *)
1003 alloca (sizeof (struct rtx_list));
1004 rtx_list_link->value = read_rtx (infile);
1005 if (next_rtx == 0)
1006 list_rtx = rtx_list_link;
1007 else
1008 next_rtx->next = rtx_list_link;
1009 next_rtx = rtx_list_link;
1010 rtx_list_link->next = 0;
1012 /* get vector length and allocate it */
1013 XVEC (return_rtx, i) = (list_counter
1014 ? rtvec_alloc (list_counter) : NULL_RTVEC);
1015 if (list_counter > 0)
1017 next_rtx = list_rtx;
1018 for (j = 0; j < list_counter; j++,
1019 next_rtx = next_rtx->next)
1020 XVECEXP (return_rtx, i, j) = next_rtx->value;
1022 /* close bracket gotten */
1024 break;
1026 case 'S':
1027 /* 'S' is an optional string: if a closeparen follows,
1028 just store NULL for this element. */
1029 c = read_skip_spaces (infile);
1030 ungetc (c, infile);
1031 if (c == ')')
1033 XSTR (return_rtx, i) = 0;
1034 break;
1037 case 's':
1039 int saw_paren = 0;
1040 register char *stringbuf;
1042 c = read_skip_spaces (infile);
1043 if (c == '(')
1045 saw_paren = 1;
1046 c = read_skip_spaces (infile);
1048 if (c != '"')
1049 fatal_expected_char (infile, '"', c);
1051 while (1)
1053 c = getc (infile); /* Read the string */
1054 if (c == '\n')
1055 read_rtx_lineno++;
1056 if (c == '\\')
1058 c = getc (infile); /* Read the string */
1059 /* \; makes stuff for a C string constant containing
1060 newline and tab. */
1061 if (c == ';')
1063 obstack_grow (&rtl_obstack, "\\n\\t", 4);
1064 continue;
1066 if (c == '\n')
1067 read_rtx_lineno++;
1069 else if (c == '"')
1070 break;
1072 obstack_1grow (&rtl_obstack, c);
1075 obstack_1grow (&rtl_obstack, 0);
1076 stringbuf = (char *) obstack_finish (&rtl_obstack);
1078 if (saw_paren)
1080 c = read_skip_spaces (infile);
1081 if (c != ')')
1082 fatal_expected_char (infile, ')', c);
1084 XSTR (return_rtx, i) = stringbuf;
1086 break;
1088 case 'w':
1089 read_name (tmp_char, infile);
1090 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1091 tmp_wide = atoi (tmp_char);
1092 #else
1093 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1094 tmp_wide = atol (tmp_char);
1095 #else
1096 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1097 But prefer not to use our hand-rolled function above either. */
1098 #if defined(HAVE_ATOLL) || !defined(HAVE_ATOQ)
1099 tmp_wide = atoll (tmp_char);
1100 #else
1101 tmp_wide = atoq (tmp_char);
1102 #endif
1103 #endif
1104 #endif
1105 XWINT (return_rtx, i) = tmp_wide;
1106 break;
1108 case 'i':
1109 case 'n':
1110 read_name (tmp_char, infile);
1111 tmp_int = atoi (tmp_char);
1112 XINT (return_rtx, i) = tmp_int;
1113 break;
1115 default:
1116 fprintf (stderr,
1117 "switch format wrong in rtl.read_rtx(). format was: %c.\n",
1118 format_ptr[-1]);
1119 fprintf (stderr, "\tfile position: %ld\n", ftell (infile));
1120 abort ();
1123 c = read_skip_spaces (infile);
1124 if (c != ')')
1125 fatal_expected_char (infile, ')', c);
1127 return return_rtx;
1130 #if defined ENABLE_RTL_CHECKING && (GCC_VERSION >= 2007)
1131 void
1132 rtl_check_failed_bounds (r, n, file, line, func)
1133 rtx r;
1134 int n;
1135 const char *file;
1136 int line;
1137 const char *func;
1139 error ("RTL check: access of elt %d of `%s' with last elt %d",
1140 n, GET_RTX_NAME (GET_CODE (r)), GET_RTX_LENGTH (GET_CODE (r))-1);
1141 fancy_abort (file, line, func);
1144 void
1145 rtl_check_failed_type1 (r, n, c1, file, line, func)
1146 rtx r;
1147 int n;
1148 int c1;
1149 const char *file;
1150 int line;
1151 const char *func;
1153 error ("RTL check: expected elt %d type '%c', have '%c' (rtx %s)",
1154 n, c1, GET_RTX_FORMAT (GET_CODE (r))[n], GET_RTX_NAME (GET_CODE (r)));
1155 fancy_abort (file, line, func);
1158 void
1159 rtl_check_failed_type2 (r, n, c1, c2, file, line, func)
1160 rtx r;
1161 int n;
1162 int c1;
1163 int c2;
1164 const char *file;
1165 int line;
1166 const char *func;
1168 error ("RTL check: expected elt %d type '%c' or '%c', have '%c' (rtx %s)",
1169 n, c1, c2,
1170 GET_RTX_FORMAT (GET_CODE (r))[n], GET_RTX_NAME (GET_CODE(r)));
1171 fancy_abort (file, line, func);
1174 void
1175 rtl_check_failed_code1 (r, code, file, line, func)
1176 rtx r;
1177 enum rtx_code code;
1178 const char *file;
1179 int line;
1180 const char *func;
1182 error ("RTL check: expected code `%s', have `%s'",
1183 GET_RTX_NAME (code), GET_RTX_NAME (GET_CODE (r)));
1184 fancy_abort (file, line, func);
1187 void
1188 rtl_check_failed_code2 (r, code1, code2, file, line, func)
1189 rtx r;
1190 enum rtx_code code1, code2;
1191 const char *file;
1192 int line;
1193 const char *func;
1195 error ("RTL check: expected code `%s' or `%s', have `%s'",
1196 GET_RTX_NAME (code1), GET_RTX_NAME (code2),
1197 GET_RTX_NAME (GET_CODE (r)));
1198 fancy_abort (file, line, func);
1201 /* XXX Maybe print the vector? */
1202 void
1203 rtvec_check_failed_bounds (r, n, file, line, func)
1204 rtvec r;
1205 int n;
1206 const char *file;
1207 int line;
1208 const char *func;
1210 error ("RTL check: access of elt %d of vector with last elt %d",
1211 n, GET_NUM_ELEM (r)-1);
1212 fancy_abort (file, line, func);
1214 #endif /* ENABLE_RTL_CHECKING */