1 /* Allocate and read RTL for GNU C Compiler.
2 Copyright (C) 1987, 1988, 1991, 1994, 1997, 1998 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. */
29 #define obstack_chunk_alloc xmalloc
30 #define obstack_chunk_free free
32 /* Obstack used for allocating RTL objects.
33 Between functions, this is the permanent_obstack.
34 While parsing and expanding a function, this is maybepermanent_obstack
35 so we can save it if it is an inline function.
36 During optimization and output, this is function_obstack. */
38 extern struct obstack *rtl_obstack;
40 /* Indexed by rtx code, gives number of operands for an rtx with that code.
41 Does NOT include rtx header data (code and links).
42 This array is initialized in init_rtl. */
44 int rtx_length[NUM_RTX_CODE + 1];
46 /* Indexed by rtx code, gives the name of that kind of rtx, as a C string. */
48 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
51 #include "rtl.def" /* rtl expressions are documented here */
56 /* Indexed by machine mode, gives the name of that machine mode.
57 This name does not include the letters "mode". */
59 #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT, WIDER) NAME,
61 char *mode_name[(int) MAX_MACHINE_MODE + 1] = {
62 #include "machmode.def"
67 /* Add an extra field to avoid a core dump if someone tries to convert
68 MAX_MACHINE_MODE to a string. */
74 /* Indexed by machine mode, gives the length of the mode, in bytes.
75 GET_MODE_CLASS uses this. */
77 #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT, WIDER) CLASS,
79 enum mode_class mode_class[(int) MAX_MACHINE_MODE] = {
80 #include "machmode.def"
85 /* Indexed by machine mode, gives the length of the mode, in bytes.
86 GET_MODE_SIZE uses this. */
88 #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT, WIDER) SIZE,
90 int mode_size[(int) MAX_MACHINE_MODE] = {
91 #include "machmode.def"
96 /* Indexed by machine mode, gives the length of the mode's subunit.
97 GET_MODE_UNIT_SIZE uses this. */
99 #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT, WIDER) UNIT,
101 int mode_unit_size[(int) MAX_MACHINE_MODE] = {
102 #include "machmode.def" /* machine modes are documented here */
107 /* Indexed by machine mode, gives next wider natural mode
108 (QI -> HI -> SI -> DI, etc.) Widening multiply instructions
111 #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT, WIDER) \
112 (unsigned char) WIDER,
114 unsigned char mode_wider_mode[(int) MAX_MACHINE_MODE] = {
115 #include "machmode.def" /* machine modes are documented here */
120 #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT, WIDER) \
121 ((SIZE) * BITS_PER_UNIT >= HOST_BITS_PER_WIDE_INT) ? ~(unsigned HOST_WIDE_INT)0 : ((unsigned HOST_WIDE_INT) 1 << (SIZE) * BITS_PER_UNIT) - 1,
123 /* Indexed by machine mode, gives mask of significant bits in mode. */
125 unsigned HOST_WIDE_INT mode_mask_array[(int) MAX_MACHINE_MODE] = {
126 #include "machmode.def"
129 /* Indexed by mode class, gives the narrowest mode for each class. */
131 enum machine_mode class_narrowest_mode[(int) MAX_MODE_CLASS];
133 /* Indexed by rtx code, gives a sequence of operand-types for
134 rtx's of that code. The sequence is a C string in which
135 each character describes one operand. */
137 char *rtx_format[] = {
139 can cause a warning message
140 "0" field is unused (or used in a phase-dependent manner)
144 "n" like "i", but prints entries from `note_insn_name'
145 "w" an integer of width HOST_BITS_PER_WIDE_INT
147 "s" a pointer to a string
149 "S" like "s", but optional:
150 the containing rtx may end before this operand
151 "e" a pointer to an rtl expression
152 prints the expression
153 "E" a pointer to a vector that points to a number of rtl expressions
154 prints a list of the rtl expressions
155 "V" like "E", but optional:
156 the containing rtx may end before this operand
157 "u" a pointer to another insn
158 prints the uid of the insn.
159 "b" is a pointer to a bitmap header.
160 "t" is a tree pointer. */
162 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
163 #include "rtl.def" /* rtl expressions are defined here */
167 /* Indexed by rtx code, gives a character representing the "class" of
168 that rtx code. See rtl.def for documentation on the defined classes. */
171 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) CLASS,
172 #include "rtl.def" /* rtl expressions are defined here */
176 /* Names for kinds of NOTEs and REG_NOTEs. */
178 char *note_insn_name[] = { 0 , "NOTE_INSN_DELETED",
179 "NOTE_INSN_BLOCK_BEG", "NOTE_INSN_BLOCK_END",
180 "NOTE_INSN_LOOP_BEG", "NOTE_INSN_LOOP_END",
181 "NOTE_INSN_FUNCTION_END", "NOTE_INSN_SETJMP",
182 "NOTE_INSN_LOOP_CONT", "NOTE_INSN_LOOP_VTOP",
183 "NOTE_INSN_PROLOGUE_END", "NOTE_INSN_EPILOGUE_BEG",
184 "NOTE_INSN_DELETED_LABEL", "NOTE_INSN_FUNCTION_BEG",
185 "NOTE_INSN_EH_REGION_BEG", "NOTE_INSN_EH_REGION_END",
186 "NOTE_REPEATED_LINE_NUMBER", "NOTE_INSN_RANGE_START",
187 "NOTE_INSN_RANGE_END", "NOTE_INSN_LIVE" };
189 char *reg_note_name[] = { "", "REG_DEAD", "REG_INC", "REG_EQUIV", "REG_WAS_0",
190 "REG_EQUAL", "REG_RETVAL", "REG_LIBCALL",
191 "REG_NONNEG", "REG_NO_CONFLICT", "REG_UNUSED",
192 "REG_CC_SETTER", "REG_CC_USER", "REG_LABEL",
193 "REG_DEP_ANTI", "REG_DEP_OUTPUT", "REG_BR_PROB",
194 "REG_EXEC_COUNT", "REG_NOALIAS", "REG_SAVE_AREA",
195 "REG_BR_PRED", "REG_EH_CONTEXT",
196 "REG_FRAME_RELATED_EXPR", "REG_EH_REGION",
199 static void dump_and_abort PROTO((int, int, FILE *)) ATTRIBUTE_NORETURN;
200 static void read_name PROTO((char *, FILE *));
202 /* Allocate an rtx vector of N elements.
203 Store the length, and initialize all elements to zero. */
212 rt = (rtvec) obstack_alloc (rtl_obstack,
213 sizeof (struct rtvec_def)
214 + (( n - 1) * sizeof (rtunion)));
216 /* clear out the vector */
217 PUT_NUM_ELEM (rt, n);
219 for (i = 0; i < n; i++)
220 rt->elem[i].rtwint = 0;
225 /* Allocate an rtx of code CODE. The CODE is stored in the rtx;
226 all the rest is initialized to zero. */
233 register struct obstack *ob = rtl_obstack;
234 register int nelts = GET_RTX_LENGTH (code);
235 register int length = sizeof (struct rtx_def)
236 + (nelts - 1) * sizeof (rtunion);
238 /* This function is called more than any other in GCC,
239 so we manipulate the obstack directly.
241 Even though rtx objects are word aligned, we may be sharing an obstack
242 with tree nodes, which may have to be double-word aligned. So align
243 our length to the alignment mask in the obstack. */
245 length = (length + ob->alignment_mask) & ~ ob->alignment_mask;
247 if (ob->chunk_limit - ob->next_free < length)
248 _obstack_newchunk (ob, length);
249 rt = (rtx)ob->object_base;
250 ob->next_free += length;
251 ob->object_base = ob->next_free;
253 /* We want to clear everything up to the FLD array. Normally, this is
254 one int, but we don't want to assume that and it isn't very portable
257 memset (rt, 0, sizeof (struct rtx_def) - sizeof (rtunion));
264 /* Free the rtx X and all RTL allocated since X. */
270 obstack_free (rtl_obstack, x);
273 /* Create a new copy of an rtx.
274 Recursively copies the operands of the rtx,
275 except for those few rtx codes that are sharable. */
283 register RTX_CODE code;
284 register char *format_ptr;
286 code = GET_CODE (orig);
299 /* SCRATCH must be shared because they represent distinct values. */
304 /* CONST can be shared if it contains a SYMBOL_REF. If it contains
305 a LABEL_REF, it isn't sharable. */
306 if (GET_CODE (XEXP (orig, 0)) == PLUS
307 && GET_CODE (XEXP (XEXP (orig, 0), 0)) == SYMBOL_REF
308 && GET_CODE (XEXP (XEXP (orig, 0), 1)) == CONST_INT)
312 /* A MEM with a constant address is not sharable. The problem is that
313 the constant address may need to be reloaded. If the mem is shared,
314 then reloading one copy of this mem will cause all copies to appear
315 to have been reloaded. */
321 copy = rtx_alloc (code);
322 PUT_MODE (copy, GET_MODE (orig));
323 copy->in_struct = orig->in_struct;
324 copy->volatil = orig->volatil;
325 copy->unchanging = orig->unchanging;
326 copy->integrated = orig->integrated;
328 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
330 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
332 switch (*format_ptr++)
335 XEXP (copy, i) = XEXP (orig, i);
336 if (XEXP (orig, i) != NULL)
337 XEXP (copy, i) = copy_rtx (XEXP (orig, i));
342 XEXP (copy, i) = XEXP (orig, i);
347 XVEC (copy, i) = XVEC (orig, i);
348 if (XVEC (orig, i) != NULL)
350 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
351 for (j = 0; j < XVECLEN (copy, i); j++)
352 XVECEXP (copy, i, j) = copy_rtx (XVECEXP (orig, i, j));
358 bitmap new_bits = BITMAP_OBSTACK_ALLOC (rtl_obstack);
359 bitmap_copy (new_bits, XBITMAP (orig, i));
360 XBITMAP (copy, i) = new_bits;
365 XTREE (copy, i) = XTREE (orig, i);
369 XWINT (copy, i) = XWINT (orig, i);
373 XINT (copy, i) = XINT (orig, i);
378 XSTR (copy, i) = XSTR (orig, i);
388 /* Similar to `copy_rtx' except that if MAY_SHARE is present, it is
389 placed in the result directly, rather than being copied. */
392 copy_most_rtx (orig, may_share)
394 register rtx may_share;
398 register RTX_CODE code;
399 register char *format_ptr;
401 if (orig == may_share)
404 code = GET_CODE (orig);
421 copy = rtx_alloc (code);
422 PUT_MODE (copy, GET_MODE (orig));
423 copy->in_struct = orig->in_struct;
424 copy->volatil = orig->volatil;
425 copy->unchanging = orig->unchanging;
426 copy->integrated = orig->integrated;
428 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
430 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
432 switch (*format_ptr++)
435 XEXP (copy, i) = XEXP (orig, i);
436 if (XEXP (orig, i) != NULL && XEXP (orig, i) != may_share)
437 XEXP (copy, i) = copy_most_rtx (XEXP (orig, i), may_share);
442 XEXP (copy, i) = XEXP (orig, i);
447 XVEC (copy, i) = XVEC (orig, i);
448 if (XVEC (orig, i) != NULL)
450 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
451 for (j = 0; j < XVECLEN (copy, i); j++)
453 = copy_most_rtx (XVECEXP (orig, i, j), may_share);
458 XWINT (copy, i) = XWINT (orig, i);
463 XINT (copy, i) = XINT (orig, i);
468 XSTR (copy, i) = XSTR (orig, i);
478 /* Subroutines of read_rtx. */
480 /* Dump code after printing a message. Used when read_rtx finds
484 dump_and_abort (expected_c, actual_c, infile)
485 int expected_c, actual_c;
492 "Expected character %c. Found character %c.",
493 expected_c, actual_c);
494 fprintf (stderr, " At file position: %ld\n", ftell (infile));
495 fprintf (stderr, "Following characters are:\n\t");
496 for (i = 0; i < 200; i++)
502 fprintf (stderr, "Aborting.\n");
506 /* Read chars from INFILE until a non-whitespace char
507 and return that. Comments, both Lisp style and C style,
508 are treated as whitespace.
509 Tools such as genflags use this function. */
512 read_skip_spaces (infile)
516 while ((c = getc (infile)))
518 if (c == ' ' || c == '\n' || c == '\t' || c == '\f')
522 while ((c = getc (infile)) && c != '\n' && c != EOF)
530 dump_and_abort ('*', c, infile);
533 while ((c = getc (infile)) && c != EOF)
535 if (prevc == '*' && c == '/')
545 /* Read an rtx code name into the buffer STR[].
546 It is terminated by any of the punctuation chars of rtx printed syntax. */
549 read_name (str, infile)
556 c = read_skip_spaces(infile);
561 if (c == ' ' || c == '\n' || c == '\t' || c == '\f')
563 if (c == ':' || c == ')' || c == ']' || c == '"' || c == '/'
564 || c == '(' || c == '[')
574 fprintf (stderr, "missing name or number");
575 dump_and_abort (-1, -1, infile);
581 /* Provide a version of a function to read a long long if the system does
583 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !defined(HAVE_ATOLL) && !defined(HAVE_ATOQ)
589 HOST_WIDE_INT tmp_wide;
601 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
602 if (new_wide < tmp_wide)
604 /* Return INT_MAX equiv on overflow. */
605 tmp_wide = (~(unsigned HOST_WIDE_INT)0) >> 1;
613 tmp_wide = -tmp_wide;
618 /* Read an rtx in printed representation from INFILE
619 and return an actual rtx in core constructed accordingly.
620 read_rtx is not used in the compiler proper, but rather in
621 the utilities gen*.c that construct C code from machine descriptions. */
627 register int i, j, list_counter;
629 register char *format_ptr;
630 /* tmp_char is a buffer used for reading decimal integers
631 and names of rtx types and machine modes.
632 Therefore, 256 must be enough. */
637 HOST_WIDE_INT tmp_wide;
639 /* Linked list structure for making RTXs: */
642 struct rtx_list *next;
643 rtx value; /* Value of this node... */
646 c = read_skip_spaces (infile); /* Should be open paren. */
648 dump_and_abort ('(', c, infile);
650 read_name (tmp_char, infile);
654 for (i=0; i < NUM_RTX_CODE; i++) /* @@ might speed this search up */
656 if (!(strcmp (tmp_char, GET_RTX_NAME (i))))
658 tmp_code = (RTX_CODE) i; /* get value for name */
662 if (tmp_code == UNKNOWN)
665 "Unknown rtx read in rtl.read_rtx(). Code name was %s .",
668 /* (NIL) stands for an expression that isn't there. */
671 /* Discard the closeparen. */
672 while ((c = getc (infile)) && c != ')');
676 return_rtx = rtx_alloc (tmp_code); /* if we end up with an insn expression
677 then we free this space below. */
678 format_ptr = GET_RTX_FORMAT (GET_CODE (return_rtx));
680 /* If what follows is `: mode ', read it and
681 store the mode in the rtx. */
683 i = read_skip_spaces (infile);
687 read_name (tmp_char, infile);
688 for (k = 0; k < NUM_MACHINE_MODES; k++)
689 if (!strcmp (GET_MODE_NAME (k), tmp_char))
692 PUT_MODE (return_rtx, (enum machine_mode) k );
697 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (return_rtx)); i++)
698 switch (*format_ptr++)
700 /* 0 means a field for internal use only.
701 Don't expect it to be present in the input. */
707 XEXP (return_rtx, i) = read_rtx (infile);
711 /* 'V' is an optional vector: if a closeparen follows,
712 just store NULL for this element. */
713 c = read_skip_spaces (infile);
717 XVEC (return_rtx, i) = 0;
720 /* Now process the vector. */
724 register struct rtx_list *next_rtx, *rtx_list_link;
725 struct rtx_list *list_rtx = NULL;
727 c = read_skip_spaces (infile);
729 dump_and_abort ('[', c, infile);
731 /* add expressions to a list, while keeping a count */
734 while ((c = read_skip_spaces (infile)) && c != ']')
738 rtx_list_link = (struct rtx_list *)
739 alloca (sizeof (struct rtx_list));
740 rtx_list_link->value = read_rtx (infile);
742 list_rtx = rtx_list_link;
744 next_rtx->next = rtx_list_link;
745 next_rtx = rtx_list_link;
746 rtx_list_link->next = 0;
748 /* get vector length and allocate it */
749 XVEC (return_rtx, i) = (list_counter
750 ? rtvec_alloc (list_counter) : NULL_RTVEC);
751 if (list_counter > 0)
754 for (j = 0; j < list_counter; j++,
755 next_rtx = next_rtx->next)
756 XVECEXP (return_rtx, i, j) = next_rtx->value;
758 /* close bracket gotten */
763 /* 'S' is an optional string: if a closeparen follows,
764 just store NULL for this element. */
765 c = read_skip_spaces (infile);
769 XSTR (return_rtx, i) = 0;
776 register char *stringbuf;
778 c = read_skip_spaces (infile);
782 c = read_skip_spaces (infile);
785 dump_and_abort ('"', c, infile);
789 c = getc (infile); /* Read the string */
792 c = getc (infile); /* Read the string */
793 /* \; makes stuff for a C string constant containing
797 obstack_grow (rtl_obstack, "\\n\\t", 4);
804 obstack_1grow (rtl_obstack, c);
807 obstack_1grow (rtl_obstack, 0);
808 stringbuf = (char *) obstack_finish (rtl_obstack);
812 c = read_skip_spaces (infile);
814 dump_and_abort (')', c, infile);
816 XSTR (return_rtx, i) = stringbuf;
821 read_name (tmp_char, infile);
822 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
823 tmp_wide = atoi (tmp_char);
825 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
826 tmp_wide = atol (tmp_char);
828 /* Prefer atoll over atoq, since the former is in the ISO C9X draft.
829 But prefer not to use our hand-rolled function above either. */
830 #if defined(HAVE_ATOLL) || !defined(HAVE_ATOQ)
831 tmp_wide = atoll (tmp_char);
833 tmp_wide = atoq (tmp_char);
837 XWINT (return_rtx, i) = tmp_wide;
842 read_name (tmp_char, infile);
843 tmp_int = atoi (tmp_char);
844 XINT (return_rtx, i) = tmp_int;
849 "switch format wrong in rtl.read_rtx(). format was: %c.\n",
851 fprintf (stderr, "\tfile position: %ld\n", ftell (infile));
855 c = read_skip_spaces (infile);
857 dump_and_abort (')', c, infile);
862 /* This is called once per compilation, before any rtx's are constructed.
863 It initializes the vector `rtx_length', the extra CC modes, if any,
864 and computes certain commonly-used modes. */
869 int min_class_size[(int) MAX_MODE_CLASS];
870 enum machine_mode mode;
873 for (i = 0; i < NUM_RTX_CODE; i++)
874 rtx_length[i] = strlen (rtx_format[i]);
876 /* Make CONST_DOUBLE bigger, if real values are bigger than
877 it normally expects to have room for.
878 Note that REAL_VALUE_TYPE is not defined by default,
879 since tree.h is not included. But the default dfn as `double'
881 #ifdef REAL_VALUE_TYPE
882 i = sizeof (REAL_VALUE_TYPE) / sizeof (rtunion) + 2;
883 if (rtx_length[(int) CONST_DOUBLE] < i)
885 char *s = (char *) xmalloc (i + 1);
886 rtx_length[(int) CONST_DOUBLE] = i;
887 rtx_format[(int) CONST_DOUBLE] = s;
890 /* Set the GET_RTX_FORMAT of CONST_DOUBLE to a string
891 of as many `w's as we now have elements. Subtract two from
892 the size to account for the 'e' and the '0'. */
893 for (i = 2; i < rtx_length[(int) CONST_DOUBLE]; i++)
899 #ifdef EXTRA_CC_MODES
900 for (i = (int) CCmode + 1; i < (int) MAX_MACHINE_MODE; i++)
902 mode_class[i] = MODE_CC;
903 mode_mask_array[i] = mode_mask_array[(int) CCmode];
904 mode_size[i] = mode_size[(int) CCmode];
905 mode_unit_size[i] = mode_unit_size[(int) CCmode];
906 mode_wider_mode[i - 1] = i;
907 mode_wider_mode[i] = (unsigned char)VOIDmode;
911 /* Find the narrowest mode for each class. */
913 for (i = 0; i < (int) MAX_MODE_CLASS; i++)
914 min_class_size[i] = 1000;
916 for (mode = VOIDmode; (int) mode < (int) MAX_MACHINE_MODE;
917 mode = (enum machine_mode) ((int) mode + 1))
919 if (GET_MODE_SIZE (mode) < min_class_size[(int) GET_MODE_CLASS (mode)])
921 class_narrowest_mode[(int) GET_MODE_CLASS (mode)] = mode;
922 min_class_size[(int) GET_MODE_CLASS (mode)] = GET_MODE_SIZE (mode);