gcse.c (do_local_cprop): Do not extend lifetimes of registers set by do_local_cprop.
[official-gcc.git] / gcc / read-rtl.c
blob03b0f7d4b0dd4d765cf2671b7c65a9a640227597
1 /* RTL reader for GNU C Compiler.
2 Copyright (C) 1987, 1988, 1991, 1994, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "hconfig.h"
23 #include "system.h"
24 #include "rtl.h"
25 #include "obstack.h"
26 #include "hashtab.h"
28 #define obstack_chunk_alloc xmalloc
29 #define obstack_chunk_free free
31 static htab_t md_constants;
33 static void fatal_with_file_and_line PARAMS ((FILE *, const char *, ...))
34 ATTRIBUTE_PRINTF_2 ATTRIBUTE_NORETURN;
35 static void fatal_expected_char PARAMS ((FILE *, int, int)) ATTRIBUTE_NORETURN;
36 static void read_name PARAMS ((char *, FILE *));
37 static char *read_string PARAMS ((struct obstack *, FILE *, int));
38 static char *read_quoted_string PARAMS ((struct obstack *, FILE *));
39 static char *read_braced_string PARAMS ((struct obstack *, FILE *));
40 static void read_escape PARAMS ((struct obstack *, FILE *));
41 static unsigned def_hash PARAMS ((const void *));
42 static int def_name_eq_p PARAMS ((const void *, const void *));
43 static void read_constants PARAMS ((FILE *infile, char *tmp_char));
44 static void validate_const_int PARAMS ((FILE *, const char *));
46 /* Subroutines of read_rtx. */
48 /* The current line number for the file. */
49 int read_rtx_lineno = 1;
51 /* The filename for aborting with file and line. */
52 const char *read_rtx_filename = "<unknown>";
54 static void
55 fatal_with_file_and_line VPARAMS ((FILE *infile, const char *msg, ...))
57 char context[64];
58 size_t i;
59 int c;
61 VA_OPEN (ap, msg);
62 VA_FIXEDARG (ap, FILE *, infile);
63 VA_FIXEDARG (ap, const char *, msg);
65 fprintf (stderr, "%s:%d: ", read_rtx_filename, read_rtx_lineno);
66 vfprintf (stderr, msg, ap);
67 putc ('\n', stderr);
69 /* Gather some following context. */
70 for (i = 0; i < sizeof (context)-1; ++i)
72 c = getc (infile);
73 if (c == EOF)
74 break;
75 if (c == '\r' || c == '\n')
76 break;
77 context[i] = c;
79 context[i] = '\0';
81 fprintf (stderr, "%s:%d: following context is `%s'\n",
82 read_rtx_filename, read_rtx_lineno, context);
84 VA_CLOSE (ap);
85 exit (1);
88 /* Dump code after printing a message. Used when read_rtx finds
89 invalid data. */
91 static void
92 fatal_expected_char (infile, expected_c, actual_c)
93 FILE *infile;
94 int expected_c, actual_c;
96 fatal_with_file_and_line (infile, "expected character `%c', found `%c'",
97 expected_c, actual_c);
100 /* Read chars from INFILE until a non-whitespace char
101 and return that. Comments, both Lisp style and C style,
102 are treated as whitespace.
103 Tools such as genflags use this function. */
106 read_skip_spaces (infile)
107 FILE *infile;
109 int c;
111 while (1)
113 c = getc (infile);
114 switch (c)
116 case '\n':
117 read_rtx_lineno++;
118 break;
120 case ' ': case '\t': case '\f': case '\r':
121 break;
123 case ';':
125 c = getc (infile);
126 while (c != '\n' && c != EOF);
127 read_rtx_lineno++;
128 break;
130 case '/':
132 int prevc;
133 c = getc (infile);
134 if (c != '*')
135 fatal_expected_char (infile, '*', c);
137 prevc = 0;
138 while ((c = getc (infile)) && c != EOF)
140 if (c == '\n')
141 read_rtx_lineno++;
142 else if (prevc == '*' && c == '/')
143 break;
144 prevc = c;
147 break;
149 default:
150 return c;
155 /* Read an rtx code name into the buffer STR[].
156 It is terminated by any of the punctuation chars of rtx printed syntax. */
158 static void
159 read_name (str, infile)
160 char *str;
161 FILE *infile;
163 char *p;
164 int c;
166 c = read_skip_spaces (infile);
168 p = str;
169 while (1)
171 if (c == ' ' || c == '\n' || c == '\t' || c == '\f' || c == '\r')
172 break;
173 if (c == ':' || c == ')' || c == ']' || c == '"' || c == '/'
174 || c == '(' || c == '[')
176 ungetc (c, infile);
177 break;
179 *p++ = c;
180 c = getc (infile);
182 if (p == str)
183 fatal_with_file_and_line (infile, "missing name or number");
184 if (c == '\n')
185 read_rtx_lineno++;
187 *p = 0;
189 if (md_constants)
191 /* Do constant expansion. */
192 struct md_constant *def;
194 p = str;
197 struct md_constant tmp_def;
199 tmp_def.name = p;
200 def = htab_find (md_constants, &tmp_def);
201 if (def)
202 p = def->value;
203 } while (def);
204 if (p != str)
205 strcpy (str, p);
209 /* Subroutine of the string readers. Handles backslash escapes.
210 Caller has read the backslash, but not placed it into the obstack. */
211 static void
212 read_escape (ob, infile)
213 struct obstack *ob;
214 FILE *infile;
216 int c = getc (infile);
218 switch (c)
220 /* Backslash-newline is replaced by nothing, as in C. */
221 case '\n':
222 read_rtx_lineno++;
223 return;
225 /* \" \' \\ are replaced by the second character. */
226 case '\\':
227 case '"':
228 case '\'':
229 break;
231 /* Standard C string escapes:
232 \a \b \f \n \r \t \v
233 \[0-7] \x
234 all are passed through to the output string unmolested.
235 In normal use these wind up in a string constant processed
236 by the C compiler, which will translate them appropriately.
237 We do not bother checking that \[0-7] are followed by up to
238 two octal digits, or that \x is followed by N hex digits.
239 \? \u \U are left out because they are not in traditional C. */
240 case 'a': case 'b': case 'f': case 'n': case 'r': case 't': case 'v':
241 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
242 case '7': case 'x':
243 obstack_1grow (ob, '\\');
244 break;
246 /* \; makes stuff for a C string constant containing
247 newline and tab. */
248 case ';':
249 obstack_grow (ob, "\\n\\t", 4);
250 return;
252 /* pass anything else through, but issue a warning. */
253 default:
254 fprintf (stderr, "%s:%d: warning: unrecognized escape \\%c\n",
255 read_rtx_filename, read_rtx_lineno, c);
256 obstack_1grow (ob, '\\');
257 break;
260 obstack_1grow (ob, c);
264 /* Read a double-quoted string onto the obstack. Caller has scanned
265 the leading quote. */
266 static char *
267 read_quoted_string (ob, infile)
268 struct obstack *ob;
269 FILE *infile;
271 int c;
273 while (1)
275 c = getc (infile); /* Read the string */
276 if (c == '\n')
277 read_rtx_lineno++;
278 else if (c == '\\')
280 read_escape (ob, infile);
281 continue;
283 else if (c == '"')
284 break;
286 obstack_1grow (ob, c);
289 obstack_1grow (ob, 0);
290 return obstack_finish (ob);
293 /* Read a braced string (a la Tcl) onto the obstack. Caller has
294 scanned the leading brace. Note that unlike quoted strings,
295 the outermost braces _are_ included in the string constant. */
296 static char *
297 read_braced_string (ob, infile)
298 struct obstack *ob;
299 FILE *infile;
301 int c;
302 int brace_depth = 1; /* caller-processed */
304 obstack_1grow (ob, '{');
305 while (brace_depth)
307 c = getc (infile); /* Read the string */
308 if (c == '\n')
309 read_rtx_lineno++;
310 else if (c == '{')
311 brace_depth++;
312 else if (c == '}')
313 brace_depth--;
314 else if (c == '\\')
316 read_escape (ob, infile);
317 continue;
320 obstack_1grow (ob, c);
323 obstack_1grow (ob, 0);
324 return obstack_finish (ob);
327 /* Read some kind of string constant. This is the high-level routine
328 used by read_rtx. It handles surrounding parentheses, leading star,
329 and dispatch to the appropriate string constant reader. */
331 static char *
332 read_string (ob, infile, star_if_braced)
333 struct obstack *ob;
334 FILE *infile;
335 int star_if_braced;
337 char *stringbuf;
338 int saw_paren = 0;
339 int c;
341 c = read_skip_spaces (infile);
342 if (c == '(')
344 saw_paren = 1;
345 c = read_skip_spaces (infile);
348 if (c == '"')
349 stringbuf = read_quoted_string (ob, infile);
350 else if (c == '{')
352 if (star_if_braced)
353 obstack_1grow (ob, '*');
354 stringbuf = read_braced_string (ob, infile);
356 else
357 fatal_with_file_and_line (infile, "expected `\"' or `{', found `%c'", c);
359 if (saw_paren)
361 c = read_skip_spaces (infile);
362 if (c != ')')
363 fatal_expected_char (infile, ')', c);
366 return stringbuf;
369 /* Provide a version of a function to read a long long if the system does
370 not provide one. */
371 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !defined(HAVE_ATOLL) && !defined(HAVE_ATOQ)
372 HOST_WIDE_INT
373 atoll (p)
374 const char *p;
376 int neg = 0;
377 HOST_WIDE_INT tmp_wide;
379 while (ISSPACE (*p))
380 p++;
381 if (*p == '-')
382 neg = 1, p++;
383 else if (*p == '+')
384 p++;
386 tmp_wide = 0;
387 while (ISDIGIT (*p))
389 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
390 if (new_wide < tmp_wide)
392 /* Return INT_MAX equiv on overflow. */
393 tmp_wide = (~(unsigned HOST_WIDE_INT) 0) >> 1;
394 break;
396 tmp_wide = new_wide;
397 p++;
400 if (neg)
401 tmp_wide = -tmp_wide;
402 return tmp_wide;
404 #endif
406 /* Given a constant definition, return a hash code for its name. */
407 static unsigned
408 def_hash (def)
409 const void *def;
411 unsigned result, i;
412 const char *string = ((const struct md_constant *) def)->name;
414 for (result = i = 0;*string++ != '\0'; i++)
415 result += ((unsigned char) *string << (i % CHAR_BIT));
416 return result;
419 /* Given two constant definitions, return true if they have the same name. */
420 static int
421 def_name_eq_p (def1, def2)
422 const void *def1, *def2;
424 return ! strcmp (((const struct md_constant *) def1)->name,
425 ((const struct md_constant *) def2)->name);
428 /* INFILE is a FILE pointer to read text from. TMP_CHAR is a buffer suitable
429 to read a name or number into. Process a define_constants directive,
430 starting with the optional space after the "define_constants". */
431 static void
432 read_constants (infile, tmp_char)
433 FILE *infile;
434 char *tmp_char;
436 int c;
437 htab_t defs;
439 c = read_skip_spaces (infile);
440 if (c != '[')
441 fatal_expected_char (infile, '[', c);
442 defs = md_constants;
443 if (! defs)
444 defs = htab_create (32, def_hash, def_name_eq_p, (htab_del) 0);
445 /* Disable constant expansion during definition processing. */
446 md_constants = 0;
447 while ( (c = read_skip_spaces (infile)) != ']')
449 struct md_constant *def;
450 void **entry_ptr;
452 if (c != '(')
453 fatal_expected_char (infile, '(', c);
454 def = xmalloc (sizeof (struct md_constant));
455 def->name = tmp_char;
456 read_name (tmp_char, infile);
457 entry_ptr = htab_find_slot (defs, def, TRUE);
458 if (! *entry_ptr)
459 def->name = xstrdup (tmp_char);
460 c = read_skip_spaces (infile);
461 ungetc (c, infile);
462 read_name (tmp_char, infile);
463 if (! *entry_ptr)
465 def->value = xstrdup (tmp_char);
466 *entry_ptr = def;
468 else
470 def = *entry_ptr;
471 if (strcmp (def->value, tmp_char))
472 fatal_with_file_and_line (infile,
473 "redefinition of %s, was %s, now %s",
474 def->name, def->value, tmp_char);
476 c = read_skip_spaces (infile);
477 if (c != ')')
478 fatal_expected_char (infile, ')', c);
480 md_constants = defs;
481 c = read_skip_spaces (infile);
482 if (c != ')')
483 fatal_expected_char (infile, ')', c);
486 /* For every constant definition, call CALLBACK with two arguments:
487 a pointer a pointer to the constant definition and INFO.
488 Stops when CALLBACK returns zero. */
489 void
490 traverse_md_constants (callback, info)
491 htab_trav callback;
492 void *info;
494 if (md_constants)
495 htab_traverse (md_constants, callback, info);
498 static void
499 validate_const_int (infile, string)
500 FILE *infile;
501 const char *string;
503 const char *cp;
504 int valid = 1;
506 cp = string;
507 while (*cp && ISSPACE (*cp))
508 cp++;
509 if (*cp == '-' || *cp == '+')
510 cp++;
511 if (*cp == 0)
512 valid = 0;
513 for (; *cp; cp++)
514 if (! ISDIGIT (*cp))
515 valid = 0;
516 if (!valid)
517 fatal_with_file_and_line (infile, "invalid decimal constant \"%s\"\n", string);
520 /* Read an rtx in printed representation from INFILE
521 and return an actual rtx in core constructed accordingly.
522 read_rtx is not used in the compiler proper, but rather in
523 the utilities gen*.c that construct C code from machine descriptions. */
526 read_rtx (infile)
527 FILE *infile;
529 int i, j;
530 RTX_CODE tmp_code;
531 const char *format_ptr;
532 /* tmp_char is a buffer used for reading decimal integers
533 and names of rtx types and machine modes.
534 Therefore, 256 must be enough. */
535 char tmp_char[256];
536 rtx return_rtx;
537 int c;
538 int tmp_int;
539 HOST_WIDE_INT tmp_wide;
541 /* Obstack used for allocating RTL objects. */
542 static struct obstack rtl_obstack;
543 static int initialized;
545 /* Linked list structure for making RTXs: */
546 struct rtx_list
548 struct rtx_list *next;
549 rtx value; /* Value of this node. */
552 if (!initialized) {
553 obstack_init (&rtl_obstack);
554 initialized = 1;
557 again:
558 c = read_skip_spaces (infile); /* Should be open paren. */
559 if (c != '(')
560 fatal_expected_char (infile, '(', c);
562 read_name (tmp_char, infile);
564 tmp_code = UNKNOWN;
566 if (! strcmp (tmp_char, "define_constants"))
568 read_constants (infile, tmp_char);
569 goto again;
571 for (i = 0; i < NUM_RTX_CODE; i++)
572 if (! strcmp (tmp_char, GET_RTX_NAME (i)))
574 tmp_code = (RTX_CODE) i; /* get value for name */
575 break;
578 if (tmp_code == UNKNOWN)
579 fatal_with_file_and_line (infile, "unknown rtx code `%s'", tmp_char);
581 /* (NIL) stands for an expression that isn't there. */
582 if (tmp_code == NIL)
584 /* Discard the closeparen. */
585 while ((c = getc (infile)) && c != ')')
588 return 0;
591 /* If we end up with an insn expression then we free this space below. */
592 return_rtx = rtx_alloc (tmp_code);
593 format_ptr = GET_RTX_FORMAT (GET_CODE (return_rtx));
595 /* If what follows is `: mode ', read it and
596 store the mode in the rtx. */
598 i = read_skip_spaces (infile);
599 if (i == ':')
601 read_name (tmp_char, infile);
602 for (j = 0; j < NUM_MACHINE_MODES; j++)
603 if (! strcmp (GET_MODE_NAME (j), tmp_char))
604 break;
606 if (j == MAX_MACHINE_MODE)
607 fatal_with_file_and_line (infile, "unknown mode `%s'", tmp_char);
609 PUT_MODE (return_rtx, (enum machine_mode) j);
611 else
612 ungetc (i, infile);
614 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (return_rtx)); i++)
615 switch (*format_ptr++)
617 /* 0 means a field for internal use only.
618 Don't expect it to be present in the input. */
619 case '0':
620 break;
622 case 'e':
623 case 'u':
624 XEXP (return_rtx, i) = read_rtx (infile);
625 break;
627 case 'V':
628 /* 'V' is an optional vector: if a closeparen follows,
629 just store NULL for this element. */
630 c = read_skip_spaces (infile);
631 ungetc (c, infile);
632 if (c == ')')
634 XVEC (return_rtx, i) = 0;
635 break;
637 /* Now process the vector. */
639 case 'E':
641 /* Obstack to store scratch vector in. */
642 struct obstack vector_stack;
643 int list_counter = 0;
644 rtvec return_vec = NULL_RTVEC;
646 c = read_skip_spaces (infile);
647 if (c != '[')
648 fatal_expected_char (infile, '[', c);
650 /* add expressions to a list, while keeping a count */
651 obstack_init (&vector_stack);
652 while ((c = read_skip_spaces (infile)) && c != ']')
654 ungetc (c, infile);
655 list_counter++;
656 obstack_ptr_grow (&vector_stack, (PTR) read_rtx (infile));
658 if (list_counter > 0)
660 return_vec = rtvec_alloc (list_counter);
661 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
662 list_counter * sizeof (rtx));
664 XVEC (return_rtx, i) = return_vec;
665 obstack_free (&vector_stack, NULL);
666 /* close bracket gotten */
668 break;
670 case 'S':
671 /* 'S' is an optional string: if a closeparen follows,
672 just store NULL for this element. */
673 c = read_skip_spaces (infile);
674 ungetc (c, infile);
675 if (c == ')')
677 XSTR (return_rtx, i) = 0;
678 break;
681 case 'T':
682 case 's':
684 char *stringbuf;
686 /* The output template slot of a DEFINE_INSN,
687 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
688 gets a star inserted as its first character, if it is
689 written with a brace block instead of a string constant. */
690 int star_if_braced = (format_ptr[-1] == 'T');
692 stringbuf = read_string (&rtl_obstack, infile, star_if_braced);
694 /* For insn patterns, we want to provide a default name
695 based on the file and line, like "*foo.md:12", if the
696 given name is blank. These are only for define_insn and
697 define_insn_and_split, to aid debugging. */
698 if (*stringbuf == '\0'
699 && i == 0
700 && (GET_CODE (return_rtx) == DEFINE_INSN
701 || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT))
703 char line_name[20];
704 const char *fn = (read_rtx_filename ? read_rtx_filename : "rtx");
705 const char *slash;
706 for (slash = fn; *slash; slash ++)
707 if (*slash == '/' || *slash == '\\' || *slash == ':')
708 fn = slash + 1;
709 obstack_1grow (&rtl_obstack, '*');
710 obstack_grow (&rtl_obstack, fn, strlen (fn));
711 sprintf (line_name, ":%d", read_rtx_lineno);
712 obstack_grow (&rtl_obstack, line_name, strlen (line_name)+1);
713 stringbuf = (char *) obstack_finish (&rtl_obstack);
716 if (star_if_braced)
717 XTMPL (return_rtx, i) = stringbuf;
718 else
719 XSTR (return_rtx, i) = stringbuf;
721 break;
723 case 'w':
724 read_name (tmp_char, infile);
725 validate_const_int (infile, tmp_char);
726 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
727 tmp_wide = atoi (tmp_char);
728 #else
729 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
730 tmp_wide = atol (tmp_char);
731 #else
732 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
733 But prefer not to use our hand-rolled function above either. */
734 #if defined(HAVE_ATOLL) || !defined(HAVE_ATOQ)
735 tmp_wide = atoll (tmp_char);
736 #else
737 tmp_wide = atoq (tmp_char);
738 #endif
739 #endif
740 #endif
741 XWINT (return_rtx, i) = tmp_wide;
742 break;
744 case 'i':
745 case 'n':
746 read_name (tmp_char, infile);
747 validate_const_int (infile, tmp_char);
748 tmp_int = atoi (tmp_char);
749 XINT (return_rtx, i) = tmp_int;
750 break;
752 default:
753 fprintf (stderr,
754 "switch format wrong in rtl.read_rtx(). format was: %c.\n",
755 format_ptr[-1]);
756 fprintf (stderr, "\tfile position: %ld\n", ftell (infile));
757 abort ();
760 c = read_skip_spaces (infile);
761 if (c != ')')
762 fatal_expected_char (infile, ')', c);
764 return return_rtx;