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. */
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 #ifdef NEED_DECLARATION_ATOL
47 /* Indexed by rtx code, gives number of operands for an rtx with that code.
48 Does NOT include rtx header data (code and links).
49 This array is initialized in init_rtl. */
51 int rtx_length[NUM_RTX_CODE + 1];
53 /* Indexed by rtx code, gives the name of that kind of rtx, as a C string. */
55 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
58 #include "rtl.def" /* rtl expressions are documented here */
63 /* Indexed by machine mode, gives the name of that machine mode.
64 This name does not include the letters "mode". */
66 #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT, WIDER) NAME,
68 char *mode_name[(int) MAX_MACHINE_MODE] = {
69 #include "machmode.def"
79 /* Indexed by machine mode, gives the length of the mode, in bytes.
80 GET_MODE_CLASS uses this. */
82 #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT, WIDER) CLASS,
84 enum mode_class mode_class[(int) MAX_MACHINE_MODE] = {
85 #include "machmode.def"
90 /* Indexed by machine mode, gives the length of the mode, in bytes.
91 GET_MODE_SIZE uses this. */
93 #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT, WIDER) SIZE,
95 int mode_size[(int) MAX_MACHINE_MODE] = {
96 #include "machmode.def"
101 /* Indexed by machine mode, gives the length of the mode's subunit.
102 GET_MODE_UNIT_SIZE uses this. */
104 #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT, WIDER) UNIT,
106 int mode_unit_size[(int) MAX_MACHINE_MODE] = {
107 #include "machmode.def" /* machine modes are documented here */
112 /* Indexed by machine mode, gives next wider natural mode
113 (QI -> HI -> SI -> DI, etc.) Widening multiply instructions
116 #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT, WIDER) \
117 (enum machine_mode) WIDER,
119 enum machine_mode mode_wider_mode[(int) MAX_MACHINE_MODE] = {
120 #include "machmode.def" /* machine modes are documented here */
125 /* Indexed by mode class, gives the narrowest mode for each class. */
127 enum machine_mode class_narrowest_mode[(int) MAX_MODE_CLASS];
129 /* Indexed by rtx code, gives a sequence of operand-types for
130 rtx's of that code. The sequence is a C string in which
131 each character describes one operand. */
133 char *rtx_format[] = {
135 can cause a warning message
136 "0" field is unused (or used in a phase-dependent manner)
140 "n" like "i", but prints entries from `note_insn_name'
141 "w" an integer of width HOST_BITS_PER_WIDE_INT
143 "s" a pointer to a string
145 "S" like "s", but optional:
146 the containing rtx may end before this operand
147 "e" a pointer to an rtl expression
148 prints the expression
149 "E" a pointer to a vector that points to a number of rtl expressions
150 prints a list of the rtl expressions
151 "V" like "E", but optional:
152 the containing rtx may end before this operand
153 "u" a pointer to another insn
154 prints the uid of the insn. */
156 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
157 #include "rtl.def" /* rtl expressions are defined here */
161 /* Indexed by rtx code, gives a character representing the "class" of
162 that rtx code. See rtl.def for documentation on the defined classes. */
165 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) CLASS,
166 #include "rtl.def" /* rtl expressions are defined here */
170 /* Names for kinds of NOTEs and REG_NOTEs. */
172 char *note_insn_name[] = { 0 , "NOTE_INSN_DELETED",
173 "NOTE_INSN_BLOCK_BEG", "NOTE_INSN_BLOCK_END",
174 "NOTE_INSN_LOOP_BEG", "NOTE_INSN_LOOP_END",
175 "NOTE_INSN_FUNCTION_END", "NOTE_INSN_SETJMP",
176 "NOTE_INSN_LOOP_CONT", "NOTE_INSN_LOOP_VTOP",
177 "NOTE_INSN_PROLOGUE_END", "NOTE_INSN_EPILOGUE_BEG",
178 "NOTE_INSN_DELETED_LABEL", "NOTE_INSN_FUNCTION_BEG",
179 "NOTE_INSN_EH_REGION_BEG", "NOTE_INSN_EH_REGION_END",
180 "NOTE_REPEATED_LINE_NUMBER" };
182 char *reg_note_name[] = { "", "REG_DEAD", "REG_INC", "REG_EQUIV", "REG_WAS_0",
183 "REG_EQUAL", "REG_RETVAL", "REG_LIBCALL",
184 "REG_NONNEG", "REG_NO_CONFLICT", "REG_UNUSED",
185 "REG_CC_SETTER", "REG_CC_USER", "REG_LABEL",
186 "REG_DEP_ANTI", "REG_DEP_OUTPUT", "REG_BR_PROB",
187 "REG_EXEC_COUNT", "REG_NOALIAS", "REG_SAVE_AREA",
188 "REG_BR_PRED", "REG_EH_CONTEXT" };
190 static void dump_and_abort PROTO((int, int, FILE *));
191 static void read_name PROTO((char *, FILE *));
193 /* Allocate an rtx vector of N elements.
194 Store the length, and initialize all elements to zero. */
203 rt = (rtvec) obstack_alloc (rtl_obstack,
204 sizeof (struct rtvec_def)
205 + (( n - 1) * sizeof (rtunion)));
207 /* clear out the vector */
208 PUT_NUM_ELEM (rt, n);
210 for (i = 0; i < n; i++)
211 rt->elem[i].rtwint = 0;
216 /* Allocate an rtx of code CODE. The CODE is stored in the rtx;
217 all the rest is initialized to zero. */
224 register struct obstack *ob = rtl_obstack;
225 register int nelts = GET_RTX_LENGTH (code);
226 register int length = sizeof (struct rtx_def)
227 + (nelts - 1) * sizeof (rtunion);
229 /* This function is called more than any other in GCC,
230 so we manipulate the obstack directly.
232 Even though rtx objects are word aligned, we may be sharing an obstack
233 with tree nodes, which may have to be double-word aligned. So align
234 our length to the alignment mask in the obstack. */
236 length = (length + ob->alignment_mask) & ~ ob->alignment_mask;
238 if (ob->chunk_limit - ob->next_free < length)
239 _obstack_newchunk (ob, length);
240 rt = (rtx)ob->object_base;
241 ob->next_free += length;
242 ob->object_base = ob->next_free;
244 /* We want to clear everything up to the FLD array. Normally, this is
245 one int, but we don't want to assume that and it isn't very portable
248 length = (sizeof (struct rtx_def) - sizeof (rtunion) - 1) / sizeof (int);
249 for (; length >= 0; length--)
250 ((int *) rt)[length] = 0;
257 /* Free the rtx X and all RTL allocated since X. */
263 obstack_free (rtl_obstack, x);
266 /* Create a new copy of an rtx.
267 Recursively copies the operands of the rtx,
268 except for those few rtx codes that are sharable. */
276 register RTX_CODE code;
277 register char *format_ptr;
279 code = GET_CODE (orig);
292 /* SCRATCH must be shared because they represent distinct values. */
297 /* CONST can be shared if it contains a SYMBOL_REF. If it contains
298 a LABEL_REF, it isn't sharable. */
299 if (GET_CODE (XEXP (orig, 0)) == PLUS
300 && GET_CODE (XEXP (XEXP (orig, 0), 0)) == SYMBOL_REF
301 && GET_CODE (XEXP (XEXP (orig, 0), 1)) == CONST_INT)
305 /* A MEM with a constant address is not sharable. The problem is that
306 the constant address may need to be reloaded. If the mem is shared,
307 then reloading one copy of this mem will cause all copies to appear
308 to have been reloaded. */
314 copy = rtx_alloc (code);
315 PUT_MODE (copy, GET_MODE (orig));
316 copy->in_struct = orig->in_struct;
317 copy->volatil = orig->volatil;
318 copy->unchanging = orig->unchanging;
319 copy->integrated = orig->integrated;
321 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
323 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
325 switch (*format_ptr++)
328 XEXP (copy, i) = XEXP (orig, i);
329 if (XEXP (orig, i) != NULL)
330 XEXP (copy, i) = copy_rtx (XEXP (orig, i));
335 XEXP (copy, i) = XEXP (orig, i);
340 XVEC (copy, i) = XVEC (orig, i);
341 if (XVEC (orig, i) != NULL)
343 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
344 for (j = 0; j < XVECLEN (copy, i); j++)
345 XVECEXP (copy, i, j) = copy_rtx (XVECEXP (orig, i, j));
350 XWINT (copy, i) = XWINT (orig, i);
354 XINT (copy, i) = XINT (orig, i);
359 XSTR (copy, i) = XSTR (orig, i);
369 /* Similar to `copy_rtx' except that if MAY_SHARE is present, it is
370 placed in the result directly, rather than being copied. */
373 copy_most_rtx (orig, may_share)
375 register rtx may_share;
379 register RTX_CODE code;
380 register char *format_ptr;
382 if (orig == may_share)
385 code = GET_CODE (orig);
402 copy = rtx_alloc (code);
403 PUT_MODE (copy, GET_MODE (orig));
404 copy->in_struct = orig->in_struct;
405 copy->volatil = orig->volatil;
406 copy->unchanging = orig->unchanging;
407 copy->integrated = orig->integrated;
409 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
411 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
413 switch (*format_ptr++)
416 XEXP (copy, i) = XEXP (orig, i);
417 if (XEXP (orig, i) != NULL && XEXP (orig, i) != may_share)
418 XEXP (copy, i) = copy_most_rtx (XEXP (orig, i), may_share);
423 XEXP (copy, i) = XEXP (orig, i);
428 XVEC (copy, i) = XVEC (orig, i);
429 if (XVEC (orig, i) != NULL)
431 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
432 for (j = 0; j < XVECLEN (copy, i); j++)
434 = copy_most_rtx (XVECEXP (orig, i, j), may_share);
439 XWINT (copy, i) = XWINT (orig, i);
444 XINT (copy, i) = XINT (orig, i);
449 XSTR (copy, i) = XSTR (orig, i);
459 /* Subroutines of read_rtx. */
461 /* Dump code after printing a message. Used when read_rtx finds
465 dump_and_abort (expected_c, actual_c, infile)
466 int expected_c, actual_c;
473 "Expected character %c. Found character %c.",
474 expected_c, actual_c);
475 fprintf (stderr, " At file position: %ld\n", ftell (infile));
476 fprintf (stderr, "Following characters are:\n\t");
477 for (i = 0; i < 200; i++)
483 fprintf (stderr, "Aborting.\n");
487 /* Read chars from INFILE until a non-whitespace char
488 and return that. Comments, both Lisp style and C style,
489 are treated as whitespace.
490 Tools such as genflags use this function. */
493 read_skip_spaces (infile)
497 while ((c = getc (infile)))
499 if (c == ' ' || c == '\n' || c == '\t' || c == '\f')
503 while ((c = getc (infile)) && c != '\n') ;
510 dump_and_abort ('*', c, infile);
513 while ((c = getc (infile)))
515 if (prevc == '*' && c == '/')
525 /* Read an rtx code name into the buffer STR[].
526 It is terminated by any of the punctuation chars of rtx printed syntax. */
529 read_name (str, infile)
536 c = read_skip_spaces(infile);
541 if (c == ' ' || c == '\n' || c == '\t' || c == '\f')
543 if (c == ':' || c == ')' || c == ']' || c == '"' || c == '/'
544 || c == '(' || c == '[')
554 fprintf (stderr, "missing name or number");
555 dump_and_abort (-1, -1, infile);
561 /* Read an rtx in printed representation from INFILE
562 and return an actual rtx in core constructed accordingly.
563 read_rtx is not used in the compiler proper, but rather in
564 the utilities gen*.c that construct C code from machine descriptions. */
570 register int i, j, list_counter;
572 register char *format_ptr;
573 /* tmp_char is a buffer used for reading decimal integers
574 and names of rtx types and machine modes.
575 Therefore, 256 must be enough. */
580 HOST_WIDE_INT tmp_wide;
582 /* Linked list structure for making RTXs: */
585 struct rtx_list *next;
586 rtx value; /* Value of this node... */
589 c = read_skip_spaces (infile); /* Should be open paren. */
591 dump_and_abort ('(', c, infile);
593 read_name (tmp_char, infile);
597 for (i=0; i < NUM_RTX_CODE; i++) /* @@ might speed this search up */
599 if (!(strcmp (tmp_char, GET_RTX_NAME (i))))
601 tmp_code = (RTX_CODE) i; /* get value for name */
605 if (tmp_code == UNKNOWN)
608 "Unknown rtx read in rtl.read_rtx(). Code name was %s .",
611 /* (NIL) stands for an expression that isn't there. */
614 /* Discard the closeparen. */
615 while ((c = getc (infile)) && c != ')');
619 return_rtx = rtx_alloc (tmp_code); /* if we end up with an insn expression
620 then we free this space below. */
621 format_ptr = GET_RTX_FORMAT (GET_CODE (return_rtx));
623 /* If what follows is `: mode ', read it and
624 store the mode in the rtx. */
626 i = read_skip_spaces (infile);
630 read_name (tmp_char, infile);
631 for (k = 0; k < NUM_MACHINE_MODES; k++)
632 if (!strcmp (GET_MODE_NAME (k), tmp_char))
635 PUT_MODE (return_rtx, (enum machine_mode) k );
640 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (return_rtx)); i++)
641 switch (*format_ptr++)
643 /* 0 means a field for internal use only.
644 Don't expect it to be present in the input. */
650 XEXP (return_rtx, i) = read_rtx (infile);
654 /* 'V' is an optional vector: if a closeparen follows,
655 just store NULL for this element. */
656 c = read_skip_spaces (infile);
660 XVEC (return_rtx, i) = 0;
663 /* Now process the vector. */
667 register struct rtx_list *next_rtx, *rtx_list_link;
668 struct rtx_list *list_rtx = NULL;
670 c = read_skip_spaces (infile);
672 dump_and_abort ('[', c, infile);
674 /* add expressions to a list, while keeping a count */
677 while ((c = read_skip_spaces (infile)) && c != ']')
681 rtx_list_link = (struct rtx_list *)
682 alloca (sizeof (struct rtx_list));
683 rtx_list_link->value = read_rtx (infile);
685 list_rtx = rtx_list_link;
687 next_rtx->next = rtx_list_link;
688 next_rtx = rtx_list_link;
689 rtx_list_link->next = 0;
691 /* get vector length and allocate it */
692 XVEC (return_rtx, i) = (list_counter
693 ? rtvec_alloc (list_counter) : NULL_RTVEC);
694 if (list_counter > 0)
697 for (j = 0; j < list_counter; j++,
698 next_rtx = next_rtx->next)
699 XVECEXP (return_rtx, i, j) = next_rtx->value;
701 /* close bracket gotten */
706 /* 'S' is an optional string: if a closeparen follows,
707 just store NULL for this element. */
708 c = read_skip_spaces (infile);
712 XSTR (return_rtx, i) = 0;
719 register char *stringbuf;
721 c = read_skip_spaces (infile);
725 c = read_skip_spaces (infile);
728 dump_and_abort ('"', c, infile);
732 c = getc (infile); /* Read the string */
735 c = getc (infile); /* Read the string */
736 /* \; makes stuff for a C string constant containing
740 obstack_grow (rtl_obstack, "\\n\\t", 4);
747 obstack_1grow (rtl_obstack, c);
750 obstack_1grow (rtl_obstack, 0);
751 stringbuf = (char *) obstack_finish (rtl_obstack);
755 c = read_skip_spaces (infile);
757 dump_and_abort (')', c, infile);
759 XSTR (return_rtx, i) = stringbuf;
764 read_name (tmp_char, infile);
765 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
766 tmp_wide = atoi (tmp_char);
768 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
769 tmp_wide = atol (tmp_char);
772 tmp_wide = atoll (tmp_char);
775 tmp_wide = atoq (tmp_char);
780 XWINT (return_rtx, i) = tmp_wide;
785 read_name (tmp_char, infile);
786 tmp_int = atoi (tmp_char);
787 XINT (return_rtx, i) = tmp_int;
792 "switch format wrong in rtl.read_rtx(). format was: %c.\n",
794 fprintf (stderr, "\tfile position: %ld\n", ftell (infile));
798 c = read_skip_spaces (infile);
800 dump_and_abort (')', c, infile);
805 /* This is called once per compilation, before any rtx's are constructed.
806 It initializes the vector `rtx_length', the extra CC modes, if any,
807 and computes certain commonly-used modes. */
812 int min_class_size[(int) MAX_MODE_CLASS];
813 enum machine_mode mode;
816 for (i = 0; i < NUM_RTX_CODE; i++)
817 rtx_length[i] = strlen (rtx_format[i]);
819 /* Make CONST_DOUBLE bigger, if real values are bigger than
820 it normally expects to have room for.
821 Note that REAL_VALUE_TYPE is not defined by default,
822 since tree.h is not included. But the default dfn as `double'
824 #ifdef REAL_VALUE_TYPE
825 i = sizeof (REAL_VALUE_TYPE) / sizeof (rtunion) + 2;
826 if (rtx_length[(int) CONST_DOUBLE] < i)
828 char *s = (char *) xmalloc (i + 1);
829 rtx_length[(int) CONST_DOUBLE] = i;
830 rtx_format[(int) CONST_DOUBLE] = s;
833 /* Set the GET_RTX_FORMAT of CONST_DOUBLE to a string
834 of as many `w's as we now have elements. Subtract two from
835 the size to account for the 'e' and the '0'. */
836 for (i = 2; i < rtx_length[(int) CONST_DOUBLE]; i++)
842 #ifdef EXTRA_CC_MODES
843 for (i = (int) CCmode + 1; i < (int) MAX_MACHINE_MODE; i++)
845 mode_class[i] = MODE_CC;
846 mode_size[i] = mode_size[(int) CCmode];
847 mode_unit_size[i] = mode_unit_size[(int) CCmode];
848 mode_wider_mode[i - 1] = (enum machine_mode) i;
849 mode_wider_mode[i] = VOIDmode;
853 /* Find the narrowest mode for each class. */
855 for (i = 0; i < (int) MAX_MODE_CLASS; i++)
856 min_class_size[i] = 1000;
858 for (mode = VOIDmode; (int) mode < (int) MAX_MACHINE_MODE;
859 mode = (enum machine_mode) ((int) mode + 1))
861 if (GET_MODE_SIZE (mode) < min_class_size[(int) GET_MODE_CLASS (mode)])
863 class_narrowest_mode[(int) GET_MODE_CLASS (mode)] = mode;
864 min_class_size[(int) GET_MODE_CLASS (mode)] = GET_MODE_SIZE (mode);