PR 10066
[official-gcc.git] / gcc / read-rtl.c
blob95bc2fefa57fc6757b37709098b8ac7cf4115386
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 "bconfig.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "obstack.h"
28 #include "hashtab.h"
30 static htab_t md_constants;
32 static void fatal_with_file_and_line PARAMS ((FILE *, const char *, ...))
33 ATTRIBUTE_PRINTF_2 ATTRIBUTE_NORETURN;
34 static void fatal_expected_char PARAMS ((FILE *, int, int)) ATTRIBUTE_NORETURN;
35 static void read_name PARAMS ((char *, FILE *));
36 static char *read_string PARAMS ((struct obstack *, FILE *, int));
37 static char *read_quoted_string PARAMS ((struct obstack *, FILE *));
38 static char *read_braced_string PARAMS ((struct obstack *, FILE *));
39 static void read_escape PARAMS ((struct obstack *, FILE *));
40 static hashval_t def_hash PARAMS ((const void *));
41 static int def_name_eq_p PARAMS ((const void *, const void *));
42 static void read_constants PARAMS ((FILE *infile, char *tmp_char));
43 static void validate_const_int PARAMS ((FILE *, const char *));
45 /* Subroutines of read_rtx. */
47 /* The current line number for the file. */
48 int read_rtx_lineno = 1;
50 /* The filename for aborting with file and line. */
51 const char *read_rtx_filename = "<unknown>";
53 static void
54 fatal_with_file_and_line VPARAMS ((FILE *infile, const char *msg, ...))
56 char context[64];
57 size_t i;
58 int c;
60 VA_OPEN (ap, msg);
61 VA_FIXEDARG (ap, FILE *, infile);
62 VA_FIXEDARG (ap, const char *, msg);
64 fprintf (stderr, "%s:%d: ", read_rtx_filename, read_rtx_lineno);
65 vfprintf (stderr, msg, ap);
66 putc ('\n', stderr);
68 /* Gather some following context. */
69 for (i = 0; i < sizeof (context)-1; ++i)
71 c = getc (infile);
72 if (c == EOF)
73 break;
74 if (c == '\r' || c == '\n')
75 break;
76 context[i] = c;
78 context[i] = '\0';
80 fprintf (stderr, "%s:%d: following context is `%s'\n",
81 read_rtx_filename, read_rtx_lineno, context);
83 VA_CLOSE (ap);
84 exit (1);
87 /* Dump code after printing a message. Used when read_rtx finds
88 invalid data. */
90 static void
91 fatal_expected_char (infile, expected_c, actual_c)
92 FILE *infile;
93 int expected_c, actual_c;
95 fatal_with_file_and_line (infile, "expected character `%c', found `%c'",
96 expected_c, actual_c);
99 /* Read chars from INFILE until a non-whitespace char
100 and return that. Comments, both Lisp style and C style,
101 are treated as whitespace.
102 Tools such as genflags use this function. */
105 read_skip_spaces (infile)
106 FILE *infile;
108 int c;
110 while (1)
112 c = getc (infile);
113 switch (c)
115 case '\n':
116 read_rtx_lineno++;
117 break;
119 case ' ': case '\t': case '\f': case '\r':
120 break;
122 case ';':
124 c = getc (infile);
125 while (c != '\n' && c != EOF);
126 read_rtx_lineno++;
127 break;
129 case '/':
131 int prevc;
132 c = getc (infile);
133 if (c != '*')
134 fatal_expected_char (infile, '*', c);
136 prevc = 0;
137 while ((c = getc (infile)) && c != EOF)
139 if (c == '\n')
140 read_rtx_lineno++;
141 else if (prevc == '*' && c == '/')
142 break;
143 prevc = c;
146 break;
148 default:
149 return c;
154 /* Read an rtx code name into the buffer STR[].
155 It is terminated by any of the punctuation chars of rtx printed syntax. */
157 static void
158 read_name (str, infile)
159 char *str;
160 FILE *infile;
162 char *p;
163 int c;
165 c = read_skip_spaces (infile);
167 p = str;
168 while (1)
170 if (c == ' ' || c == '\n' || c == '\t' || c == '\f' || c == '\r')
171 break;
172 if (c == ':' || c == ')' || c == ']' || c == '"' || c == '/'
173 || c == '(' || c == '[')
175 ungetc (c, infile);
176 break;
178 *p++ = c;
179 c = getc (infile);
181 if (p == str)
182 fatal_with_file_and_line (infile, "missing name or number");
183 if (c == '\n')
184 read_rtx_lineno++;
186 *p = 0;
188 if (md_constants)
190 /* Do constant expansion. */
191 struct md_constant *def;
193 p = str;
196 struct md_constant tmp_def;
198 tmp_def.name = p;
199 def = htab_find (md_constants, &tmp_def);
200 if (def)
201 p = def->value;
202 } while (def);
203 if (p != str)
204 strcpy (str, p);
208 /* Subroutine of the string readers. Handles backslash escapes.
209 Caller has read the backslash, but not placed it into the obstack. */
210 static void
211 read_escape (ob, infile)
212 struct obstack *ob;
213 FILE *infile;
215 int c = getc (infile);
217 switch (c)
219 /* Backslash-newline is replaced by nothing, as in C. */
220 case '\n':
221 read_rtx_lineno++;
222 return;
224 /* \" \' \\ are replaced by the second character. */
225 case '\\':
226 case '"':
227 case '\'':
228 break;
230 /* Standard C string escapes:
231 \a \b \f \n \r \t \v
232 \[0-7] \x
233 all are passed through to the output string unmolested.
234 In normal use these wind up in a string constant processed
235 by the C compiler, which will translate them appropriately.
236 We do not bother checking that \[0-7] are followed by up to
237 two octal digits, or that \x is followed by N hex digits.
238 \? \u \U are left out because they are not in traditional C. */
239 case 'a': case 'b': case 'f': case 'n': case 'r': case 't': case 'v':
240 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
241 case '7': case 'x':
242 obstack_1grow (ob, '\\');
243 break;
245 /* \; makes stuff for a C string constant containing
246 newline and tab. */
247 case ';':
248 obstack_grow (ob, "\\n\\t", 4);
249 return;
251 /* pass anything else through, but issue a warning. */
252 default:
253 fprintf (stderr, "%s:%d: warning: unrecognized escape \\%c\n",
254 read_rtx_filename, read_rtx_lineno, c);
255 obstack_1grow (ob, '\\');
256 break;
259 obstack_1grow (ob, c);
263 /* Read a double-quoted string onto the obstack. Caller has scanned
264 the leading quote. */
265 static char *
266 read_quoted_string (ob, infile)
267 struct obstack *ob;
268 FILE *infile;
270 int c;
272 while (1)
274 c = getc (infile); /* Read the string */
275 if (c == '\n')
276 read_rtx_lineno++;
277 else if (c == '\\')
279 read_escape (ob, infile);
280 continue;
282 else if (c == '"')
283 break;
285 obstack_1grow (ob, c);
288 obstack_1grow (ob, 0);
289 return obstack_finish (ob);
292 /* Read a braced string (a la Tcl) onto the obstack. Caller has
293 scanned the leading brace. Note that unlike quoted strings,
294 the outermost braces _are_ included in the string constant. */
295 static char *
296 read_braced_string (ob, infile)
297 struct obstack *ob;
298 FILE *infile;
300 int c;
301 int brace_depth = 1; /* caller-processed */
303 obstack_1grow (ob, '{');
304 while (brace_depth)
306 c = getc (infile); /* Read the string */
307 if (c == '\n')
308 read_rtx_lineno++;
309 else if (c == '{')
310 brace_depth++;
311 else if (c == '}')
312 brace_depth--;
313 else if (c == '\\')
315 read_escape (ob, infile);
316 continue;
319 obstack_1grow (ob, c);
322 obstack_1grow (ob, 0);
323 return obstack_finish (ob);
326 /* Read some kind of string constant. This is the high-level routine
327 used by read_rtx. It handles surrounding parentheses, leading star,
328 and dispatch to the appropriate string constant reader. */
330 static char *
331 read_string (ob, infile, star_if_braced)
332 struct obstack *ob;
333 FILE *infile;
334 int star_if_braced;
336 char *stringbuf;
337 int saw_paren = 0;
338 int c;
340 c = read_skip_spaces (infile);
341 if (c == '(')
343 saw_paren = 1;
344 c = read_skip_spaces (infile);
347 if (c == '"')
348 stringbuf = read_quoted_string (ob, infile);
349 else if (c == '{')
351 if (star_if_braced)
352 obstack_1grow (ob, '*');
353 stringbuf = read_braced_string (ob, infile);
355 else
356 fatal_with_file_and_line (infile, "expected `\"' or `{', found `%c'", c);
358 if (saw_paren)
360 c = read_skip_spaces (infile);
361 if (c != ')')
362 fatal_expected_char (infile, ')', c);
365 return stringbuf;
368 /* Provide a version of a function to read a long long if the system does
369 not provide one. */
370 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !defined(HAVE_ATOLL) && !defined(HAVE_ATOQ)
371 HOST_WIDE_INT atoll PARAMS ((const char *));
373 HOST_WIDE_INT
374 atoll (p)
375 const char *p;
377 int neg = 0;
378 HOST_WIDE_INT tmp_wide;
380 while (ISSPACE (*p))
381 p++;
382 if (*p == '-')
383 neg = 1, p++;
384 else if (*p == '+')
385 p++;
387 tmp_wide = 0;
388 while (ISDIGIT (*p))
390 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
391 if (new_wide < tmp_wide)
393 /* Return INT_MAX equiv on overflow. */
394 tmp_wide = (~(unsigned HOST_WIDE_INT) 0) >> 1;
395 break;
397 tmp_wide = new_wide;
398 p++;
401 if (neg)
402 tmp_wide = -tmp_wide;
403 return tmp_wide;
405 #endif
407 /* Given a constant definition, return a hash code for its name. */
408 static hashval_t
409 def_hash (def)
410 const void *def;
412 unsigned result, i;
413 const char *string = ((const struct md_constant *) def)->name;
415 for (result = i = 0;*string++ != '\0'; i++)
416 result += ((unsigned char) *string << (i % CHAR_BIT));
417 return result;
420 /* Given two constant definitions, return true if they have the same name. */
421 static int
422 def_name_eq_p (def1, def2)
423 const void *def1, *def2;
425 return ! strcmp (((const struct md_constant *) def1)->name,
426 ((const struct md_constant *) def2)->name);
429 /* INFILE is a FILE pointer to read text from. TMP_CHAR is a buffer suitable
430 to read a name or number into. Process a define_constants directive,
431 starting with the optional space after the "define_constants". */
432 static void
433 read_constants (infile, tmp_char)
434 FILE *infile;
435 char *tmp_char;
437 int c;
438 htab_t defs;
440 c = read_skip_spaces (infile);
441 if (c != '[')
442 fatal_expected_char (infile, '[', c);
443 defs = md_constants;
444 if (! defs)
445 defs = htab_create (32, def_hash, def_name_eq_p, (htab_del) 0);
446 /* Disable constant expansion during definition processing. */
447 md_constants = 0;
448 while ( (c = read_skip_spaces (infile)) != ']')
450 struct md_constant *def;
451 void **entry_ptr;
453 if (c != '(')
454 fatal_expected_char (infile, '(', c);
455 def = xmalloc (sizeof (struct md_constant));
456 def->name = tmp_char;
457 read_name (tmp_char, infile);
458 entry_ptr = htab_find_slot (defs, def, TRUE);
459 if (! *entry_ptr)
460 def->name = xstrdup (tmp_char);
461 c = read_skip_spaces (infile);
462 ungetc (c, infile);
463 read_name (tmp_char, infile);
464 if (! *entry_ptr)
466 def->value = xstrdup (tmp_char);
467 *entry_ptr = def;
469 else
471 def = *entry_ptr;
472 if (strcmp (def->value, tmp_char))
473 fatal_with_file_and_line (infile,
474 "redefinition of %s, was %s, now %s",
475 def->name, def->value, tmp_char);
477 c = read_skip_spaces (infile);
478 if (c != ')')
479 fatal_expected_char (infile, ')', c);
481 md_constants = defs;
482 c = read_skip_spaces (infile);
483 if (c != ')')
484 fatal_expected_char (infile, ')', c);
487 /* For every constant definition, call CALLBACK with two arguments:
488 a pointer a pointer to the constant definition and INFO.
489 Stops when CALLBACK returns zero. */
490 void
491 traverse_md_constants (callback, info)
492 htab_trav callback;
493 void *info;
495 if (md_constants)
496 htab_traverse (md_constants, callback, info);
499 static void
500 validate_const_int (infile, string)
501 FILE *infile;
502 const char *string;
504 const char *cp;
505 int valid = 1;
507 cp = string;
508 while (*cp && ISSPACE (*cp))
509 cp++;
510 if (*cp == '-' || *cp == '+')
511 cp++;
512 if (*cp == 0)
513 valid = 0;
514 for (; *cp; cp++)
515 if (! ISDIGIT (*cp))
516 valid = 0;
517 if (!valid)
518 fatal_with_file_and_line (infile, "invalid decimal constant \"%s\"\n", string);
521 /* Read an rtx in printed representation from INFILE
522 and return an actual rtx in core constructed accordingly.
523 read_rtx is not used in the compiler proper, but rather in
524 the utilities gen*.c that construct C code from machine descriptions. */
527 read_rtx (infile)
528 FILE *infile;
530 int i, j;
531 RTX_CODE tmp_code;
532 const char *format_ptr;
533 /* tmp_char is a buffer used for reading decimal integers
534 and names of rtx types and machine modes.
535 Therefore, 256 must be enough. */
536 char tmp_char[256];
537 rtx return_rtx;
538 int c;
539 int tmp_int;
540 HOST_WIDE_INT tmp_wide;
542 /* Obstack used for allocating RTL objects. */
543 static struct obstack rtl_obstack;
544 static int initialized;
546 /* Linked list structure for making RTXs: */
547 struct rtx_list
549 struct rtx_list *next;
550 rtx value; /* Value of this node. */
553 if (!initialized) {
554 obstack_init (&rtl_obstack);
555 initialized = 1;
558 again:
559 c = read_skip_spaces (infile); /* Should be open paren. */
560 if (c != '(')
561 fatal_expected_char (infile, '(', c);
563 read_name (tmp_char, infile);
565 tmp_code = UNKNOWN;
567 if (! strcmp (tmp_char, "define_constants"))
569 read_constants (infile, tmp_char);
570 goto again;
572 for (i = 0; i < NUM_RTX_CODE; i++)
573 if (! strcmp (tmp_char, GET_RTX_NAME (i)))
575 tmp_code = (RTX_CODE) i; /* get value for name */
576 break;
579 if (tmp_code == UNKNOWN)
580 fatal_with_file_and_line (infile, "unknown rtx code `%s'", tmp_char);
582 /* (NIL) stands for an expression that isn't there. */
583 if (tmp_code == NIL)
585 /* Discard the closeparen. */
586 while ((c = getc (infile)) && c != ')')
589 return 0;
592 /* If we end up with an insn expression then we free this space below. */
593 return_rtx = rtx_alloc (tmp_code);
594 format_ptr = GET_RTX_FORMAT (GET_CODE (return_rtx));
596 /* If what follows is `: mode ', read it and
597 store the mode in the rtx. */
599 i = read_skip_spaces (infile);
600 if (i == ':')
602 read_name (tmp_char, infile);
603 for (j = 0; j < NUM_MACHINE_MODES; j++)
604 if (! strcmp (GET_MODE_NAME (j), tmp_char))
605 break;
607 if (j == MAX_MACHINE_MODE)
608 fatal_with_file_and_line (infile, "unknown mode `%s'", tmp_char);
610 PUT_MODE (return_rtx, (enum machine_mode) j);
612 else
613 ungetc (i, infile);
615 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (return_rtx)); i++)
616 switch (*format_ptr++)
618 /* 0 means a field for internal use only.
619 Don't expect it to be present in the input. */
620 case '0':
621 break;
623 case 'e':
624 case 'u':
625 XEXP (return_rtx, i) = read_rtx (infile);
626 break;
628 case 'V':
629 /* 'V' is an optional vector: if a closeparen follows,
630 just store NULL for this element. */
631 c = read_skip_spaces (infile);
632 ungetc (c, infile);
633 if (c == ')')
635 XVEC (return_rtx, i) = 0;
636 break;
638 /* Now process the vector. */
640 case 'E':
642 /* Obstack to store scratch vector in. */
643 struct obstack vector_stack;
644 int list_counter = 0;
645 rtvec return_vec = NULL_RTVEC;
647 c = read_skip_spaces (infile);
648 if (c != '[')
649 fatal_expected_char (infile, '[', c);
651 /* add expressions to a list, while keeping a count */
652 obstack_init (&vector_stack);
653 while ((c = read_skip_spaces (infile)) && c != ']')
655 ungetc (c, infile);
656 list_counter++;
657 obstack_ptr_grow (&vector_stack, (PTR) read_rtx (infile));
659 if (list_counter > 0)
661 return_vec = rtvec_alloc (list_counter);
662 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
663 list_counter * sizeof (rtx));
665 XVEC (return_rtx, i) = return_vec;
666 obstack_free (&vector_stack, NULL);
667 /* close bracket gotten */
669 break;
671 case 'S':
672 /* 'S' is an optional string: if a closeparen follows,
673 just store NULL for this element. */
674 c = read_skip_spaces (infile);
675 ungetc (c, infile);
676 if (c == ')')
678 XSTR (return_rtx, i) = 0;
679 break;
682 case 'T':
683 case 's':
685 char *stringbuf;
687 /* The output template slot of a DEFINE_INSN,
688 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
689 gets a star inserted as its first character, if it is
690 written with a brace block instead of a string constant. */
691 int star_if_braced = (format_ptr[-1] == 'T');
693 stringbuf = read_string (&rtl_obstack, infile, star_if_braced);
695 /* For insn patterns, we want to provide a default name
696 based on the file and line, like "*foo.md:12", if the
697 given name is blank. These are only for define_insn and
698 define_insn_and_split, to aid debugging. */
699 if (*stringbuf == '\0'
700 && i == 0
701 && (GET_CODE (return_rtx) == DEFINE_INSN
702 || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT))
704 char line_name[20];
705 const char *fn = (read_rtx_filename ? read_rtx_filename : "rtx");
706 const char *slash;
707 for (slash = fn; *slash; slash ++)
708 if (*slash == '/' || *slash == '\\' || *slash == ':')
709 fn = slash + 1;
710 obstack_1grow (&rtl_obstack, '*');
711 obstack_grow (&rtl_obstack, fn, strlen (fn));
712 sprintf (line_name, ":%d", read_rtx_lineno);
713 obstack_grow (&rtl_obstack, line_name, strlen (line_name)+1);
714 stringbuf = (char *) obstack_finish (&rtl_obstack);
717 if (star_if_braced)
718 XTMPL (return_rtx, i) = stringbuf;
719 else
720 XSTR (return_rtx, i) = stringbuf;
722 break;
724 case 'w':
725 read_name (tmp_char, infile);
726 validate_const_int (infile, tmp_char);
727 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
728 tmp_wide = atoi (tmp_char);
729 #else
730 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
731 tmp_wide = atol (tmp_char);
732 #else
733 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
734 But prefer not to use our hand-rolled function above either. */
735 #if defined(HAVE_ATOLL) || !defined(HAVE_ATOQ)
736 tmp_wide = atoll (tmp_char);
737 #else
738 tmp_wide = atoq (tmp_char);
739 #endif
740 #endif
741 #endif
742 XWINT (return_rtx, i) = tmp_wide;
743 break;
745 case 'i':
746 case 'n':
747 read_name (tmp_char, infile);
748 validate_const_int (infile, tmp_char);
749 tmp_int = atoi (tmp_char);
750 XINT (return_rtx, i) = tmp_int;
751 break;
753 default:
754 fprintf (stderr,
755 "switch format wrong in rtl.read_rtx(). format was: %c.\n",
756 format_ptr[-1]);
757 fprintf (stderr, "\tfile position: %ld\n", ftell (infile));
758 abort ();
761 c = read_skip_spaces (infile);
762 if (c != ')')
763 fatal_expected_char (infile, ')', c);
765 return return_rtx;