1 /* Allocate and read RTL for GNU C Compiler.
2 Copyright (C) 1987-1991 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
27 #define obstack_chunk_alloc xmalloc
28 #define obstack_chunk_free free
29 extern int xmalloc ();
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;
42 /* Indexed by rtx code, gives number of operands for an rtx with that code.
43 Does NOT include rtx header data (code and links).
44 This array is initialized in init_rtl. */
46 int rtx_length[NUM_RTX_CODE + 1];
48 /* Indexed by rtx code, gives the name of that kind of rtx, as a C string. */
50 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
53 #include "rtl.def" /* rtl expressions are documented here */
58 /* Indexed by machine mode, gives the name of that machine mode.
59 This name does not include the letters "mode". */
61 #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT, WIDER) NAME,
63 char *mode_name[(int) MAX_MACHINE_MODE] = {
64 #include "machmode.def"
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 (enum machine_mode) WIDER,
114 enum machine_mode mode_wider_mode[(int) MAX_MACHINE_MODE] = {
115 #include "machmode.def" /* machine modes are documented here */
120 /* Indexed by mode class, gives the narrowest mode for each class. */
122 enum machine_mode class_narrowest_mode[(int) MAX_MODE_CLASS];
124 /* Commonly used modes. */
126 enum machine_mode byte_mode; /* Mode whose width is BITS_PER_UNIT */
127 enum machine_mode word_mode; /* Mode whose width is BITS_PER_WORD */
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 charcter 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 "s" a pointer to a string
143 "S" like "s", but optional:
144 the containing rtx may end before this operand
145 "e" a pointer to an rtl expression
146 prints the expression
147 "E" a pointer to a vector that points to a number of rtl expressions
148 prints a list of the rtl expressions
149 "V" like "E", but optional:
150 the containing rtx may end before this operand
151 "u" a pointer to another insn
152 prints the uid of the insn. */
154 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
155 #include "rtl.def" /* rtl expressions are defined here */
159 /* Indexed by rtx code, gives a character representing the "class" of
160 that rtx code. See rtl.def for documentation on the defined classes. */
163 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) CLASS,
164 #include "rtl.def" /* rtl expressions are defined here */
168 /* Names for kinds of NOTEs and REG_NOTEs. */
170 char *note_insn_name[] = { "NOTE_INSN_FUNCTION_BEG", "NOTE_INSN_DELETED",
171 "NOTE_INSN_BLOCK_BEG", "NOTE_INSN_BLOCK_END",
172 "NOTE_INSN_LOOP_BEG", "NOTE_INSN_LOOP_END",
173 "NOTE_INSN_FUNCTION_END", "NOTE_INSN_SETJMP",
174 "NOTE_INSN_LOOP_CONT", "NOTE_INSN_LOOP_VTOP" };
176 char *reg_note_name[] = { "", "REG_DEAD", "REG_INC", "REG_EQUIV", "REG_WAS_0",
177 "REG_EQUAL", "REG_RETVAL", "REG_LIBCALL",
178 "REG_NONNEG", "REG_NO_CONFLICT", "REG_UNUSED",
179 "REG_CC_SETTER", "REG_CC_USER", "REG_LABEL",
180 "REG_DEP_ANTI", "REG_DEP_OUTPUT" };
182 /* Allocate an rtx vector of N elements.
183 Store the length, and initialize all elements to zero. */
192 rt = (rtvec) obstack_alloc (rtl_obstack,
193 sizeof (struct rtvec_def)
194 + (( n - 1) * sizeof (rtunion)));
196 /* clear out the vector */
198 for (i=0; i < n; i++)
199 rt->elem[i].rtvec = NULL; /* @@ not portable due to rtunion */
204 /* Allocate an rtx of code CODE. The CODE is stored in the rtx;
205 all the rest is initialized to zero. */
212 register struct obstack *ob = rtl_obstack;
213 register int nelts = GET_RTX_LENGTH (code);
214 register int length = sizeof (struct rtx_def)
215 + (nelts - 1) * sizeof (rtunion);
217 /* This function is called more than any other in GCC,
218 so we manipulate the obstack directly.
220 Even though rtx objects are word aligned, we may be sharing an obstack
221 with tree nodes, which may have to be double-word aligned. So align
222 our length to the alignment mask in the obstack. */
224 length = (length + ob->alignment_mask) & ~ ob->alignment_mask;
226 if (ob->chunk_limit - ob->next_free < length)
227 _obstack_newchunk (ob, length);
228 rt = (rtx)ob->object_base;
229 ob->next_free += length;
230 ob->object_base = ob->next_free;
238 /* Create a new copy of an rtx.
239 Recursively copies the operands of the rtx,
240 except for those few rtx codes that are sharable. */
248 register RTX_CODE code;
249 register char *format_ptr;
251 code = GET_CODE (orig);
266 copy = rtx_alloc (code);
267 PUT_MODE (copy, GET_MODE (orig));
268 copy->in_struct = orig->in_struct;
269 copy->volatil = orig->volatil;
270 copy->unchanging = orig->unchanging;
271 copy->integrated = orig->integrated;
273 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
275 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
277 switch (*format_ptr++)
280 XEXP (copy, i) = XEXP (orig, i);
281 if (XEXP (orig, i) != NULL)
282 XEXP (copy, i) = copy_rtx (XEXP (orig, i));
287 XVEC (copy, i) = XVEC (orig, i);
288 if (XVEC (orig, i) != NULL)
290 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
291 for (j = 0; j < XVECLEN (copy, i); j++)
292 XVECEXP (copy, i, j) = copy_rtx (XVECEXP (orig, i, j));
297 XINT (copy, i) = XINT (orig, i);
304 /* Similar to `copy_rtx' except that if MAY_SHARE is present, it is
305 placed in the result directly, rather than being copied. */
308 copy_most_rtx (orig, may_share)
310 register rtx may_share;
314 register RTX_CODE code;
315 register char *format_ptr;
317 if (orig == may_share)
320 code = GET_CODE (orig);
335 copy = rtx_alloc (code);
336 PUT_MODE (copy, GET_MODE (orig));
337 copy->in_struct = orig->in_struct;
338 copy->volatil = orig->volatil;
339 copy->unchanging = orig->unchanging;
340 copy->integrated = orig->integrated;
342 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
344 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
346 switch (*format_ptr++)
349 XEXP (copy, i) = XEXP (orig, i);
350 if (XEXP (orig, i) != NULL && XEXP (orig, i) != may_share)
351 XEXP (copy, i) = copy_most_rtx (XEXP (orig, i), may_share);
356 XVEC (copy, i) = XVEC (orig, i);
357 if (XVEC (orig, i) != NULL)
359 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
360 for (j = 0; j < XVECLEN (copy, i); j++)
362 = copy_most_rtx (XVECEXP (orig, i, j), may_share);
367 XINT (copy, i) = XINT (orig, i);
374 /* Helper functions for instruction scheduling. */
376 /* Add ELEM wrapped in an INSN_LIST with reg note kind DEP_TYPE to the
377 LOG_LINKS of INSN, if not already there. DEP_TYPE indicates the type
378 of dependence that this link represents. */
381 add_dependence (insn, elem, dep_type)
384 enum reg_note dep_type;
388 /* Don't depend an insn on itself. */
392 /* If elem is part of a sequence that must be scheduled together, then
393 make the dependence point to the last insn of the sequence. */
394 if (NEXT_INSN (elem) && SCHED_GROUP_P (NEXT_INSN (elem)))
396 while (NEXT_INSN (elem) && SCHED_GROUP_P (NEXT_INSN (elem)))
397 elem = NEXT_INSN (elem);
398 /* Again, don't depend an insn of itself. */
403 /* Check that we don't already have this dependence. */
404 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
405 if (XEXP (link, 0) == elem)
407 /* If this is a more restrictive type of dependence than the existing
408 one, then change the existing dependence to this type. */
409 if ((int) dep_type < (int) REG_NOTE_KIND (link))
410 PUT_REG_NOTE_KIND (link, dep_type);
413 /* Might want to check one level of transitivity to save conses. */
415 link = rtx_alloc (INSN_LIST);
416 /* Insn dependency, not data dependency. */
417 PUT_REG_NOTE_KIND (link, dep_type);
418 XEXP (link, 0) = elem;
419 XEXP (link, 1) = LOG_LINKS (insn);
420 LOG_LINKS (insn) = link;
423 /* Remove ELEM wrapped in an INSN_LIST from the LOG_LINKS
424 of INSN. Abort if not found. */
426 remove_dependence (insn, elem)
433 for (prev = 0, link = LOG_LINKS (insn); link;
434 prev = link, link = XEXP (link, 1))
436 if (XEXP (link, 0) == elem)
439 XEXP (prev, 1) = XEXP (link, 1);
441 LOG_LINKS (insn) = XEXP (link, 1);
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') ;
502 dump_and_abort ('*', c, infile);
505 while (c = getc (infile))
507 if (prevc == '*' && c == '/')
517 /* Read an rtx code name into the buffer STR[].
518 It is terminated by any of the punctuation chars of rtx printed syntax. */
521 read_name (str, infile)
528 c = read_skip_spaces(infile);
533 if (c == ' ' || c == '\n' || c == '\t' || c == '\f')
535 if (c == ':' || c == ')' || c == ']' || c == '"' || c == '/'
536 || c == '(' || c == '[')
546 fprintf (stderr, "missing name or number");
547 dump_and_abort (-1, -1, infile);
553 /* Read an rtx in printed representation from INFILE
554 and return an actual rtx in core constructed accordingly.
555 read_rtx is not used in the compiler proper, but rather in
556 the utilities gen*.c that construct C code from machine descriptions. */
562 register int i, j, list_counter;
564 register char *format_ptr;
565 /* tmp_char is a buffer used for reading decimal integers
566 and names of rtx types and machine modes.
567 Therefore, 256 must be enough. */
573 /* Linked list structure for making RTXs: */
576 struct rtx_list *next;
577 rtx value; /* Value of this node... */
580 c = read_skip_spaces (infile); /* Should be open paren. */
582 dump_and_abort ('(', c, infile);
584 read_name (tmp_char, infile);
588 for (i=0; i < NUM_RTX_CODE; i++) /* @@ might speed this search up */
590 if (!(strcmp (tmp_char, GET_RTX_NAME (i))))
592 tmp_code = (RTX_CODE) i; /* get value for name */
596 if (tmp_code == UNKNOWN)
599 "Unknown rtx read in rtl.read_rtx(). Code name was %s .",
602 /* (NIL) stands for an expression that isn't there. */
605 /* Discard the closeparen. */
606 while ((c = getc (infile)) && c != ')');
610 return_rtx = rtx_alloc (tmp_code); /* if we end up with an insn expression
611 then we free this space below. */
612 format_ptr = GET_RTX_FORMAT (GET_CODE (return_rtx));
614 /* If what follows is `: mode ', read it and
615 store the mode in the rtx. */
617 i = read_skip_spaces (infile);
621 read_name (tmp_char, infile);
622 for (k = 0; k < NUM_MACHINE_MODES; k++)
623 if (!strcmp (GET_MODE_NAME (k), tmp_char))
626 PUT_MODE (return_rtx, (enum machine_mode) k );
631 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (return_rtx)); i++)
632 switch (*format_ptr++)
634 /* 0 means a field for internal use only.
635 Don't expect it to be present in the input. */
641 XEXP (return_rtx, i) = read_rtx (infile);
645 /* 'V' is an optional vector: if a closeparen follows,
646 just store NULL for this element. */
647 c = read_skip_spaces (infile);
651 XVEC (return_rtx, i) = 0;
654 /* Now process the vector. */
658 register struct rtx_list *next_rtx, *rtx_list_link;
659 struct rtx_list *list_rtx;
661 c = read_skip_spaces (infile);
663 dump_and_abort ('[', c, infile);
665 /* add expressions to a list, while keeping a count */
668 while ((c = read_skip_spaces (infile)) && c != ']')
672 rtx_list_link = (struct rtx_list *)
673 alloca (sizeof (struct rtx_list));
674 rtx_list_link->value = read_rtx (infile);
676 list_rtx = rtx_list_link;
678 next_rtx->next = rtx_list_link;
679 next_rtx = rtx_list_link;
680 rtx_list_link->next = 0;
682 /* get vector length and allocate it */
683 XVEC (return_rtx, i) = (list_counter
684 ? rtvec_alloc (list_counter)
685 : (struct rtvec_def *) NULL);
686 if (list_counter > 0)
689 for (j = 0; j < list_counter; j++,
690 next_rtx = next_rtx->next)
691 XVECEXP (return_rtx, i, j) = next_rtx->value;
693 /* close bracket gotten */
698 /* 'S' is an optional string: if a closeparen follows,
699 just store NULL for this element. */
700 c = read_skip_spaces (infile);
704 XSTR (return_rtx, i) = 0;
711 register char *stringbuf;
714 c = read_skip_spaces (infile);
718 c = read_skip_spaces (infile);
721 dump_and_abort ('"', c, infile);
724 stringbuf = (char *) xmalloc (stringbufsize + 1);
728 if (j >= stringbufsize - 4)
731 stringbuf = (char *) xrealloc (stringbuf, stringbufsize + 1);
733 stringbuf[j] = getc (infile); /* Read the string */
734 if (stringbuf[j] == '\\')
736 stringbuf[j] = getc (infile); /* Read the string */
737 /* \; makes stuff for a C string constant containing
739 if (stringbuf[j] == ';')
741 strcpy (&stringbuf[j], "\\n\\t");
745 else if (stringbuf[j] == '"')
750 stringbuf[j] = 0; /* NUL terminate the string */
751 stringbuf = (char *) xrealloc (stringbuf, j + 1);
755 c = read_skip_spaces (infile);
757 dump_and_abort (')', c, infile);
759 XSTR (return_rtx, i) = stringbuf;
765 read_name (tmp_char, infile);
766 tmp_int = atoi (tmp_char);
767 XINT (return_rtx, i) = tmp_int;
772 "switch format wrong in rtl.read_rtx(). format was: %c.\n",
774 fprintf (stderr, "\tfile position: %ld\n", ftell (infile));
778 c = read_skip_spaces (infile);
780 dump_and_abort (')', c, infile);
785 /* This is called once per compilation, before any rtx's are constructed.
786 It initializes the vector `rtx_length', the extra CC modes, if any,
787 and computes certain commonly-used modes. */
792 int min_class_size[(int) MAX_MODE_CLASS];
793 enum machine_mode mode;
796 for (i = 0; i < NUM_RTX_CODE; i++)
797 rtx_length[i] = strlen (rtx_format[i]);
799 /* Make CONST_DOUBLE bigger, if real values are bigger than
800 it normally expects to have room for.
801 Note that REAL_VALUE_TYPE is not defined by default,
802 since tree.h is not included. But the default dfn as `double'
804 #ifdef REAL_VALUE_TYPE
805 i = sizeof (REAL_VALUE_TYPE) / sizeof (rtunion) + 2;
806 if (rtx_length[(int) CONST_DOUBLE] < i)
808 char *s = (char *) xmalloc (i + 1);
809 rtx_length[(int) CONST_DOUBLE] = i;
810 rtx_format[(int) CONST_DOUBLE] = s;
813 /* Set the GET_RTX_FORMAT of CONST_DOUBLE to a string
814 of as many `i's as we now have elements. */
815 for (i = 0; i < rtx_length[(int) CONST_DOUBLE]; i++)
821 #ifdef EXTRA_CC_MODES
822 for (i = (int) CCmode + 1; i < (int) MAX_MACHINE_MODE; i++)
824 mode_class[i] = MODE_CC;
825 mode_size[i] = mode_size[(int) CCmode];
826 mode_unit_size[i] = mode_unit_size[(int) CCmode];
827 mode_wider_mode[i - 1] = (enum machine_mode) i;
828 mode_wider_mode[i] = VOIDmode;
832 /* Find the narrowest mode for each class and compute the word and byte
835 for (i = 0; i < (int) MAX_MODE_CLASS; i++)
836 min_class_size[i] = 1000;
838 for (mode = VOIDmode; (int) mode < (int) MAX_MACHINE_MODE;
839 mode = (enum machine_mode) ((int) mode + 1))
841 if (GET_MODE_SIZE (mode) < min_class_size[(int) GET_MODE_CLASS (mode)])
843 class_narrowest_mode[(int) GET_MODE_CLASS (mode)] = mode;
844 min_class_size[(int) GET_MODE_CLASS (mode)] = GET_MODE_SIZE (mode);
846 if (GET_MODE_CLASS (mode) == MODE_INT
847 && GET_MODE_BITSIZE (mode) == BITS_PER_UNIT)
850 if (GET_MODE_CLASS (mode) == MODE_INT
851 && GET_MODE_BITSIZE (mode) == BITS_PER_WORD)