1 /* Allocate and read RTL for GNU C Compiler.
2 Copyright (C) 1987, 1988, 1991, 1994, 1997 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
28 #define obstack_chunk_alloc xmalloc
29 #define obstack_chunk_free free
31 /* Obstack used for allocating RTL objects.
32 Between functions, this is the permanent_obstack.
33 While parsing and expanding a function, this is maybepermanent_obstack
34 so we can save it if it is an inline function.
35 During optimization and output, this is function_obstack. */
37 extern struct obstack *rtl_obstack;
39 /* Indexed by rtx code, gives number of operands for an rtx with that code.
40 Does NOT include rtx header data (code and links).
41 This array is initialized in init_rtl. */
43 int rtx_length[NUM_RTX_CODE + 1];
45 /* Indexed by rtx code, gives the name of that kind of rtx, as a C string. */
47 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
50 #include "rtl.def" /* rtl expressions are documented here */
55 /* Indexed by machine mode, gives the name of that machine mode.
56 This name does not include the letters "mode". */
58 #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT, WIDER) NAME,
60 char *mode_name[(int) MAX_MACHINE_MODE] = {
61 #include "machmode.def"
71 /* Indexed by machine mode, gives the length of the mode, in bytes.
72 GET_MODE_CLASS uses this. */
74 #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT, WIDER) CLASS,
76 enum mode_class mode_class[(int) MAX_MACHINE_MODE] = {
77 #include "machmode.def"
82 /* Indexed by machine mode, gives the length of the mode, in bytes.
83 GET_MODE_SIZE uses this. */
85 #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT, WIDER) SIZE,
87 int mode_size[(int) MAX_MACHINE_MODE] = {
88 #include "machmode.def"
93 /* Indexed by machine mode, gives the length of the mode's subunit.
94 GET_MODE_UNIT_SIZE uses this. */
96 #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT, WIDER) UNIT,
98 int mode_unit_size[(int) MAX_MACHINE_MODE] = {
99 #include "machmode.def" /* machine modes are documented here */
104 /* Indexed by machine mode, gives next wider natural mode
105 (QI -> HI -> SI -> DI, etc.) Widening multiply instructions
108 #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT, WIDER) \
109 (enum machine_mode) WIDER,
111 enum machine_mode mode_wider_mode[(int) MAX_MACHINE_MODE] = {
112 #include "machmode.def" /* machine modes are documented here */
117 /* Indexed by mode class, gives the narrowest mode for each class. */
119 enum machine_mode class_narrowest_mode[(int) MAX_MODE_CLASS];
121 /* Indexed by rtx code, gives a sequence of operand-types for
122 rtx's of that code. The sequence is a C string in which
123 each character describes one operand. */
125 char *rtx_format[] = {
127 can cause a warning message
128 "0" field is unused (or used in a phase-dependent manner)
132 "n" like "i", but prints entries from `note_insn_name'
133 "w" an integer of width HOST_BITS_PER_WIDE_INT
135 "s" a pointer to a string
137 "S" like "s", but optional:
138 the containing rtx may end before this operand
139 "e" a pointer to an rtl expression
140 prints the expression
141 "E" a pointer to a vector that points to a number of rtl expressions
142 prints a list of the rtl expressions
143 "V" like "E", but optional:
144 the containing rtx may end before this operand
145 "u" a pointer to another insn
146 prints the uid of the insn. */
148 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
149 #include "rtl.def" /* rtl expressions are defined here */
153 /* Indexed by rtx code, gives a character representing the "class" of
154 that rtx code. See rtl.def for documentation on the defined classes. */
157 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) CLASS,
158 #include "rtl.def" /* rtl expressions are defined here */
162 /* Names for kinds of NOTEs and REG_NOTEs. */
164 char *note_insn_name[] = { 0 , "NOTE_INSN_DELETED",
165 "NOTE_INSN_BLOCK_BEG", "NOTE_INSN_BLOCK_END",
166 "NOTE_INSN_LOOP_BEG", "NOTE_INSN_LOOP_END",
167 "NOTE_INSN_FUNCTION_END", "NOTE_INSN_SETJMP",
168 "NOTE_INSN_LOOP_CONT", "NOTE_INSN_LOOP_VTOP",
169 "NOTE_INSN_PROLOGUE_END", "NOTE_INSN_EPILOGUE_BEG",
170 "NOTE_INSN_DELETED_LABEL", "NOTE_INSN_FUNCTION_BEG",
171 "NOTE_INSN_EH_REGION_BEG", "NOTE_INSN_EH_REGION_END",
172 "NOTE_REPEATED_LINE_NUMBER" };
174 char *reg_note_name[] = { "", "REG_DEAD", "REG_INC", "REG_EQUIV", "REG_WAS_0",
175 "REG_EQUAL", "REG_RETVAL", "REG_LIBCALL",
176 "REG_NONNEG", "REG_NO_CONFLICT", "REG_UNUSED",
177 "REG_CC_SETTER", "REG_CC_USER", "REG_LABEL",
178 "REG_DEP_ANTI", "REG_DEP_OUTPUT", "REG_BR_PROB",
179 "REG_EXEC_COUNT", "REG_NOALIAS", "REG_SAVE_AREA",
180 "REG_BR_PRED", "REG_EH_CONTEXT" };
182 static void dump_and_abort PROTO((int, int, FILE *));
183 static void read_name PROTO((char *, FILE *));
185 /* Allocate an rtx vector of N elements.
186 Store the length, and initialize all elements to zero. */
195 rt = (rtvec) obstack_alloc (rtl_obstack,
196 sizeof (struct rtvec_def)
197 + (( n - 1) * sizeof (rtunion)));
199 /* clear out the vector */
200 PUT_NUM_ELEM (rt, n);
202 for (i = 0; i < n; i++)
203 rt->elem[i].rtwint = 0;
208 /* Allocate an rtx of code CODE. The CODE is stored in the rtx;
209 all the rest is initialized to zero. */
216 register struct obstack *ob = rtl_obstack;
217 register int nelts = GET_RTX_LENGTH (code);
218 register int length = sizeof (struct rtx_def)
219 + (nelts - 1) * sizeof (rtunion);
221 /* This function is called more than any other in GCC,
222 so we manipulate the obstack directly.
224 Even though rtx objects are word aligned, we may be sharing an obstack
225 with tree nodes, which may have to be double-word aligned. So align
226 our length to the alignment mask in the obstack. */
228 length = (length + ob->alignment_mask) & ~ ob->alignment_mask;
230 if (ob->chunk_limit - ob->next_free < length)
231 _obstack_newchunk (ob, length);
232 rt = (rtx)ob->object_base;
233 ob->next_free += length;
234 ob->object_base = ob->next_free;
236 /* We want to clear everything up to the FLD array. Normally, this is
237 one int, but we don't want to assume that and it isn't very portable
240 length = (sizeof (struct rtx_def) - sizeof (rtunion) - 1) / sizeof (int);
241 for (; length >= 0; length--)
242 ((int *) rt)[length] = 0;
249 /* Free the rtx X and all RTL allocated since X. */
255 obstack_free (rtl_obstack, x);
258 /* Create a new copy of an rtx.
259 Recursively copies the operands of the rtx,
260 except for those few rtx codes that are sharable. */
268 register RTX_CODE code;
269 register char *format_ptr;
271 code = GET_CODE (orig);
284 /* SCRATCH must be shared because they represent distinct values. */
289 /* CONST can be shared if it contains a SYMBOL_REF. If it contains
290 a LABEL_REF, it isn't sharable. */
291 if (GET_CODE (XEXP (orig, 0)) == PLUS
292 && GET_CODE (XEXP (XEXP (orig, 0), 0)) == SYMBOL_REF
293 && GET_CODE (XEXP (XEXP (orig, 0), 1)) == CONST_INT)
297 /* A MEM with a constant address is not sharable. The problem is that
298 the constant address may need to be reloaded. If the mem is shared,
299 then reloading one copy of this mem will cause all copies to appear
300 to have been reloaded. */
306 copy = rtx_alloc (code);
307 PUT_MODE (copy, GET_MODE (orig));
308 copy->in_struct = orig->in_struct;
309 copy->volatil = orig->volatil;
310 copy->unchanging = orig->unchanging;
311 copy->integrated = orig->integrated;
313 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
315 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
317 switch (*format_ptr++)
320 XEXP (copy, i) = XEXP (orig, i);
321 if (XEXP (orig, i) != NULL)
322 XEXP (copy, i) = copy_rtx (XEXP (orig, i));
327 XEXP (copy, i) = XEXP (orig, i);
332 XVEC (copy, i) = XVEC (orig, i);
333 if (XVEC (orig, i) != NULL)
335 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
336 for (j = 0; j < XVECLEN (copy, i); j++)
337 XVECEXP (copy, i, j) = copy_rtx (XVECEXP (orig, i, j));
342 XWINT (copy, i) = XWINT (orig, i);
346 XINT (copy, i) = XINT (orig, i);
351 XSTR (copy, i) = XSTR (orig, i);
361 /* Similar to `copy_rtx' except that if MAY_SHARE is present, it is
362 placed in the result directly, rather than being copied. */
365 copy_most_rtx (orig, may_share)
367 register rtx may_share;
371 register RTX_CODE code;
372 register char *format_ptr;
374 if (orig == may_share)
377 code = GET_CODE (orig);
394 copy = rtx_alloc (code);
395 PUT_MODE (copy, GET_MODE (orig));
396 copy->in_struct = orig->in_struct;
397 copy->volatil = orig->volatil;
398 copy->unchanging = orig->unchanging;
399 copy->integrated = orig->integrated;
401 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
403 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
405 switch (*format_ptr++)
408 XEXP (copy, i) = XEXP (orig, i);
409 if (XEXP (orig, i) != NULL && XEXP (orig, i) != may_share)
410 XEXP (copy, i) = copy_most_rtx (XEXP (orig, i), may_share);
415 XEXP (copy, i) = XEXP (orig, i);
420 XVEC (copy, i) = XVEC (orig, i);
421 if (XVEC (orig, i) != NULL)
423 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
424 for (j = 0; j < XVECLEN (copy, i); j++)
426 = copy_most_rtx (XVECEXP (orig, i, j), may_share);
431 XWINT (copy, i) = XWINT (orig, i);
436 XINT (copy, i) = XINT (orig, i);
441 XSTR (copy, i) = XSTR (orig, i);
451 /* Subroutines of read_rtx. */
453 /* Dump code after printing a message. Used when read_rtx finds
457 dump_and_abort (expected_c, actual_c, infile)
458 int expected_c, actual_c;
465 "Expected character %c. Found character %c.",
466 expected_c, actual_c);
467 fprintf (stderr, " At file position: %ld\n", ftell (infile));
468 fprintf (stderr, "Following characters are:\n\t");
469 for (i = 0; i < 200; i++)
475 fprintf (stderr, "Aborting.\n");
479 /* Read chars from INFILE until a non-whitespace char
480 and return that. Comments, both Lisp style and C style,
481 are treated as whitespace.
482 Tools such as genflags use this function. */
485 read_skip_spaces (infile)
489 while ((c = getc (infile)))
491 if (c == ' ' || c == '\n' || c == '\t' || c == '\f')
495 while ((c = getc (infile)) && c != '\n' && c != EOF)
503 dump_and_abort ('*', c, infile);
506 while ((c = getc (infile)) && c != EOF)
508 if (prevc == '*' && c == '/')
518 /* Read an rtx code name into the buffer STR[].
519 It is terminated by any of the punctuation chars of rtx printed syntax. */
522 read_name (str, infile)
529 c = read_skip_spaces(infile);
534 if (c == ' ' || c == '\n' || c == '\t' || c == '\f')
536 if (c == ':' || c == ')' || c == ']' || c == '"' || c == '/'
537 || c == '(' || c == '[')
547 fprintf (stderr, "missing name or number");
548 dump_and_abort (-1, -1, infile);
554 /* Provide a version of a function to read a long long if the system does
556 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !defined(HAVE_ATOLL) && !defined(HAVE_ATOQ)
562 HOST_WIDE_INT tmp_wide;
574 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
575 if (new_wide < tmp_wide)
577 /* Return INT_MAX equiv on overflow. */
578 tmp_wide = (~(unsigned HOST_WIDE_INT)0) >> 1;
586 tmp_wide = -tmp_wide;
591 /* Read an rtx in printed representation from INFILE
592 and return an actual rtx in core constructed accordingly.
593 read_rtx is not used in the compiler proper, but rather in
594 the utilities gen*.c that construct C code from machine descriptions. */
600 register int i, j, list_counter;
602 register char *format_ptr;
603 /* tmp_char is a buffer used for reading decimal integers
604 and names of rtx types and machine modes.
605 Therefore, 256 must be enough. */
610 HOST_WIDE_INT tmp_wide;
612 /* Linked list structure for making RTXs: */
615 struct rtx_list *next;
616 rtx value; /* Value of this node... */
619 c = read_skip_spaces (infile); /* Should be open paren. */
621 dump_and_abort ('(', c, infile);
623 read_name (tmp_char, infile);
627 for (i=0; i < NUM_RTX_CODE; i++) /* @@ might speed this search up */
629 if (!(strcmp (tmp_char, GET_RTX_NAME (i))))
631 tmp_code = (RTX_CODE) i; /* get value for name */
635 if (tmp_code == UNKNOWN)
638 "Unknown rtx read in rtl.read_rtx(). Code name was %s .",
641 /* (NIL) stands for an expression that isn't there. */
644 /* Discard the closeparen. */
645 while ((c = getc (infile)) && c != ')');
649 return_rtx = rtx_alloc (tmp_code); /* if we end up with an insn expression
650 then we free this space below. */
651 format_ptr = GET_RTX_FORMAT (GET_CODE (return_rtx));
653 /* If what follows is `: mode ', read it and
654 store the mode in the rtx. */
656 i = read_skip_spaces (infile);
660 read_name (tmp_char, infile);
661 for (k = 0; k < NUM_MACHINE_MODES; k++)
662 if (!strcmp (GET_MODE_NAME (k), tmp_char))
665 PUT_MODE (return_rtx, (enum machine_mode) k );
670 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (return_rtx)); i++)
671 switch (*format_ptr++)
673 /* 0 means a field for internal use only.
674 Don't expect it to be present in the input. */
680 XEXP (return_rtx, i) = read_rtx (infile);
684 /* 'V' is an optional vector: if a closeparen follows,
685 just store NULL for this element. */
686 c = read_skip_spaces (infile);
690 XVEC (return_rtx, i) = 0;
693 /* Now process the vector. */
697 register struct rtx_list *next_rtx, *rtx_list_link;
698 struct rtx_list *list_rtx = NULL;
700 c = read_skip_spaces (infile);
702 dump_and_abort ('[', c, infile);
704 /* add expressions to a list, while keeping a count */
707 while ((c = read_skip_spaces (infile)) && c != ']')
711 rtx_list_link = (struct rtx_list *)
712 alloca (sizeof (struct rtx_list));
713 rtx_list_link->value = read_rtx (infile);
715 list_rtx = rtx_list_link;
717 next_rtx->next = rtx_list_link;
718 next_rtx = rtx_list_link;
719 rtx_list_link->next = 0;
721 /* get vector length and allocate it */
722 XVEC (return_rtx, i) = (list_counter
723 ? rtvec_alloc (list_counter) : NULL_RTVEC);
724 if (list_counter > 0)
727 for (j = 0; j < list_counter; j++,
728 next_rtx = next_rtx->next)
729 XVECEXP (return_rtx, i, j) = next_rtx->value;
731 /* close bracket gotten */
736 /* 'S' is an optional string: if a closeparen follows,
737 just store NULL for this element. */
738 c = read_skip_spaces (infile);
742 XSTR (return_rtx, i) = 0;
749 register char *stringbuf;
751 c = read_skip_spaces (infile);
755 c = read_skip_spaces (infile);
758 dump_and_abort ('"', c, infile);
762 c = getc (infile); /* Read the string */
765 c = getc (infile); /* Read the string */
766 /* \; makes stuff for a C string constant containing
770 obstack_grow (rtl_obstack, "\\n\\t", 4);
777 obstack_1grow (rtl_obstack, c);
780 obstack_1grow (rtl_obstack, 0);
781 stringbuf = (char *) obstack_finish (rtl_obstack);
785 c = read_skip_spaces (infile);
787 dump_and_abort (')', c, infile);
789 XSTR (return_rtx, i) = stringbuf;
794 read_name (tmp_char, infile);
795 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
796 tmp_wide = atoi (tmp_char);
798 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
799 tmp_wide = atol (tmp_char);
801 /* Prefer atoll over atoq, since the former is in the ISO C9X draft.
802 But prefer not to use our hand-rolled function above either. */
803 #if defined(HAVE_ATOLL) || !defined(HAVE_ATOQ)
804 tmp_wide = atoll (tmp_char);
806 tmp_wide = atoq (tmp_char);
810 XWINT (return_rtx, i) = tmp_wide;
815 read_name (tmp_char, infile);
816 tmp_int = atoi (tmp_char);
817 XINT (return_rtx, i) = tmp_int;
822 "switch format wrong in rtl.read_rtx(). format was: %c.\n",
824 fprintf (stderr, "\tfile position: %ld\n", ftell (infile));
828 c = read_skip_spaces (infile);
830 dump_and_abort (')', c, infile);
835 /* This is called once per compilation, before any rtx's are constructed.
836 It initializes the vector `rtx_length', the extra CC modes, if any,
837 and computes certain commonly-used modes. */
842 int min_class_size[(int) MAX_MODE_CLASS];
843 enum machine_mode mode;
846 for (i = 0; i < NUM_RTX_CODE; i++)
847 rtx_length[i] = strlen (rtx_format[i]);
849 /* Make CONST_DOUBLE bigger, if real values are bigger than
850 it normally expects to have room for.
851 Note that REAL_VALUE_TYPE is not defined by default,
852 since tree.h is not included. But the default dfn as `double'
854 #ifdef REAL_VALUE_TYPE
855 i = sizeof (REAL_VALUE_TYPE) / sizeof (rtunion) + 2;
856 if (rtx_length[(int) CONST_DOUBLE] < i)
858 char *s = (char *) xmalloc (i + 1);
859 rtx_length[(int) CONST_DOUBLE] = i;
860 rtx_format[(int) CONST_DOUBLE] = s;
863 /* Set the GET_RTX_FORMAT of CONST_DOUBLE to a string
864 of as many `w's as we now have elements. Subtract two from
865 the size to account for the 'e' and the '0'. */
866 for (i = 2; i < rtx_length[(int) CONST_DOUBLE]; i++)
872 #ifdef EXTRA_CC_MODES
873 for (i = (int) CCmode + 1; i < (int) MAX_MACHINE_MODE; i++)
875 mode_class[i] = MODE_CC;
876 mode_size[i] = mode_size[(int) CCmode];
877 mode_unit_size[i] = mode_unit_size[(int) CCmode];
878 mode_wider_mode[i - 1] = (enum machine_mode) i;
879 mode_wider_mode[i] = VOIDmode;
883 /* Find the narrowest mode for each class. */
885 for (i = 0; i < (int) MAX_MODE_CLASS; i++)
886 min_class_size[i] = 1000;
888 for (mode = VOIDmode; (int) mode < (int) MAX_MACHINE_MODE;
889 mode = (enum machine_mode) ((int) mode + 1))
891 if (GET_MODE_SIZE (mode) < min_class_size[(int) GET_MODE_CLASS (mode)])
893 class_narrowest_mode[(int) GET_MODE_CLASS (mode)] = mode;
894 min_class_size[(int) GET_MODE_CLASS (mode)] = GET_MODE_SIZE (mode);