2 Copyright (C) 1987, 1988, 1991, 1994, 1997, 1998, 1999, 2000, 2001, 2002,
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include "coretypes.h"
31 static htab_t md_constants;
33 static void fatal_with_file_and_line (FILE *, const char *, ...)
34 ATTRIBUTE_PRINTF_2 ATTRIBUTE_NORETURN;
35 static void fatal_expected_char (FILE *, int, int) ATTRIBUTE_NORETURN;
36 static void read_name (char *, FILE *);
37 static char *read_string (FILE *, int);
38 static char *read_quoted_string (FILE *);
39 static char *read_braced_string (FILE *);
40 static void read_escape (FILE *);
41 static hashval_t def_hash (const void *);
42 static int def_name_eq_p (const void *, const void *);
43 static void read_constants (FILE *infile, char *tmp_char);
44 static void validate_const_int (FILE *, const char *);
46 /* Obstack used for allocating RTL strings. */
47 static struct obstack string_obstack;
49 /* Subroutines of read_rtx. */
51 /* The current line number for the file. */
52 int read_rtx_lineno = 1;
54 /* The filename for aborting with file and line. */
55 const char *read_rtx_filename = "<unknown>";
58 fatal_with_file_and_line (FILE *infile, const char *msg, ...)
67 fprintf (stderr, "%s:%d: ", read_rtx_filename, read_rtx_lineno);
68 vfprintf (stderr, msg, ap);
71 /* Gather some following context. */
72 for (i = 0; i < sizeof (context)-1; ++i)
77 if (c == '\r' || c == '\n')
83 fprintf (stderr, "%s:%d: following context is `%s'\n",
84 read_rtx_filename, read_rtx_lineno, context);
90 /* Dump code after printing a message. Used when read_rtx finds
94 fatal_expected_char (FILE *infile, int expected_c, int 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 (FILE *infile)
119 case ' ': case '\t': case '\f': case '\r':
125 while (c != '\n' && c != EOF);
134 fatal_expected_char (infile, '*', c);
137 while ((c = getc (infile)) && c != EOF)
141 else if (prevc == '*' && 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. */
158 read_name (char *str, FILE *infile)
163 c = read_skip_spaces (infile);
168 if (c == ' ' || c == '\n' || c == '\t' || c == '\f' || c == '\r')
170 if (c == ':' || c == ')' || c == ']' || c == '"' || c == '/'
171 || c == '(' || c == '[')
180 fatal_with_file_and_line (infile, "missing name or number");
188 /* Do constant expansion. */
189 struct md_constant *def;
194 struct md_constant tmp_def;
197 def = (struct md_constant *) htab_find (md_constants, &tmp_def);
206 /* Subroutine of the string readers. Handles backslash escapes.
207 Caller has read the backslash, but not placed it into the obstack. */
209 read_escape (FILE *infile)
211 int c = getc (infile);
215 /* Backslash-newline is replaced by nothing, as in C. */
220 /* \" \' \\ are replaced by the second character. */
226 /* Standard C string escapes:
229 all are passed through to the output string unmolested.
230 In normal use these wind up in a string constant processed
231 by the C compiler, which will translate them appropriately.
232 We do not bother checking that \[0-7] are followed by up to
233 two octal digits, or that \x is followed by N hex digits.
234 \? \u \U are left out because they are not in traditional C. */
235 case 'a': case 'b': case 'f': case 'n': case 'r': case 't': case 'v':
236 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
238 obstack_1grow (&string_obstack, '\\');
241 /* \; makes stuff for a C string constant containing
244 obstack_grow (&string_obstack, "\\n\\t", 4);
247 /* pass anything else through, but issue a warning. */
249 fprintf (stderr, "%s:%d: warning: unrecognized escape \\%c\n",
250 read_rtx_filename, read_rtx_lineno, c);
251 obstack_1grow (&string_obstack, '\\');
255 obstack_1grow (&string_obstack, c);
259 /* Read a double-quoted string onto the obstack. Caller has scanned
260 the leading quote. */
262 read_quoted_string (FILE *infile)
268 c = getc (infile); /* Read the string */
273 read_escape (infile);
279 obstack_1grow (&string_obstack, c);
282 obstack_1grow (&string_obstack, 0);
283 return (char *) obstack_finish (&string_obstack);
286 /* Read a braced string (a la Tcl) onto the string obstack. Caller
287 has scanned the leading brace. Note that unlike quoted strings,
288 the outermost braces _are_ included in the string constant. */
290 read_braced_string (FILE *infile)
293 int brace_depth = 1; /* caller-processed */
294 unsigned long starting_read_rtx_lineno = read_rtx_lineno;
296 obstack_1grow (&string_obstack, '{');
299 c = getc (infile); /* Read the string */
309 read_escape (infile);
313 fatal_with_file_and_line
314 (infile, "missing closing } for opening brace on line %lu",
315 starting_read_rtx_lineno);
317 obstack_1grow (&string_obstack, c);
320 obstack_1grow (&string_obstack, 0);
321 return (char *) obstack_finish (&string_obstack);
324 /* Read some kind of string constant. This is the high-level routine
325 used by read_rtx. It handles surrounding parentheses, leading star,
326 and dispatch to the appropriate string constant reader. */
329 read_string (FILE *infile, int star_if_braced)
335 c = read_skip_spaces (infile);
339 c = read_skip_spaces (infile);
343 stringbuf = read_quoted_string (infile);
347 obstack_1grow (&string_obstack, '*');
348 stringbuf = read_braced_string (infile);
351 fatal_with_file_and_line (infile, "expected `\"' or `{', found `%c'", c);
355 c = read_skip_spaces (infile);
357 fatal_expected_char (infile, ')', c);
363 /* Provide a version of a function to read a long long if the system does
365 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !defined(HAVE_ATOLL) && !defined(HAVE_ATOQ)
366 HOST_WIDE_INT atoll (const char *);
369 atoll (const char *p)
372 HOST_WIDE_INT tmp_wide;
384 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
385 if (new_wide < tmp_wide)
387 /* Return INT_MAX equiv on overflow. */
388 tmp_wide = (~(unsigned HOST_WIDE_INT) 0) >> 1;
396 tmp_wide = -tmp_wide;
401 /* Given a constant definition, return a hash code for its name. */
403 def_hash (const void *def)
406 const char *string = ((const struct md_constant *) def)->name;
408 for (result = i = 0;*string++ != '\0'; i++)
409 result += ((unsigned char) *string << (i % CHAR_BIT));
413 /* Given two constant definitions, return true if they have the same name. */
415 def_name_eq_p (const void *def1, const void *def2)
417 return ! strcmp (((const struct md_constant *) def1)->name,
418 ((const struct md_constant *) def2)->name);
421 /* INFILE is a FILE pointer to read text from. TMP_CHAR is a buffer suitable
422 to read a name or number into. Process a define_constants directive,
423 starting with the optional space after the "define_constants". */
425 read_constants (FILE *infile, char *tmp_char)
430 c = read_skip_spaces (infile);
432 fatal_expected_char (infile, '[', c);
435 defs = htab_create (32, def_hash, def_name_eq_p, (htab_del) 0);
436 /* Disable constant expansion during definition processing. */
438 while ( (c = read_skip_spaces (infile)) != ']')
440 struct md_constant *def;
444 fatal_expected_char (infile, '(', c);
445 def = XNEW (struct md_constant);
446 def->name = tmp_char;
447 read_name (tmp_char, infile);
448 entry_ptr = htab_find_slot (defs, def, INSERT);
450 def->name = xstrdup (tmp_char);
451 c = read_skip_spaces (infile);
453 read_name (tmp_char, infile);
456 def->value = xstrdup (tmp_char);
461 def = (struct md_constant *) *entry_ptr;
462 if (strcmp (def->value, tmp_char))
463 fatal_with_file_and_line (infile,
464 "redefinition of %s, was %s, now %s",
465 def->name, def->value, tmp_char);
467 c = read_skip_spaces (infile);
469 fatal_expected_char (infile, ')', c);
472 c = read_skip_spaces (infile);
474 fatal_expected_char (infile, ')', c);
477 /* For every constant definition, call CALLBACK with two arguments:
478 a pointer a pointer to the constant definition and INFO.
479 Stops when CALLBACK returns zero. */
481 traverse_md_constants (htab_trav callback, void *info)
484 htab_traverse (md_constants, callback, info);
488 validate_const_int (FILE *infile, const char *string)
494 while (*cp && ISSPACE (*cp))
496 if (*cp == '-' || *cp == '+')
504 fatal_with_file_and_line (infile, "invalid decimal constant \"%s\"\n", string);
507 /* Read an rtx in printed representation from INFILE
508 and return an actual rtx in core constructed accordingly.
509 read_rtx is not used in the compiler proper, but rather in
510 the utilities gen*.c that construct C code from machine descriptions. */
513 read_rtx (FILE *infile)
517 const char *format_ptr;
518 /* tmp_char is a buffer used for reading decimal integers
519 and names of rtx types and machine modes.
520 Therefore, 256 must be enough. */
525 HOST_WIDE_INT tmp_wide;
527 static int initialized;
529 /* Linked list structure for making RTXs: */
532 struct rtx_list *next;
533 rtx value; /* Value of this node. */
538 obstack_init (&string_obstack);
543 c = read_skip_spaces (infile); /* Should be open paren. */
545 fatal_expected_char (infile, '(', c);
547 read_name (tmp_char, infile);
550 for (i = 0; i < NUM_RTX_CODE; i++)
551 if (! strcmp (tmp_char, GET_RTX_NAME (i)))
553 tmp_code = (RTX_CODE) i; /* get value for name */
557 if (tmp_code == UNKNOWN)
559 /* (nil) stands for an expression that isn't there. */
560 if (! strcmp (tmp_char, "nil"))
562 /* Discard the closeparen. */
563 c = read_skip_spaces (infile);
565 fatal_expected_char (infile, ')', c);
568 /* (define_constants ...) has special syntax. */
569 else if (! strcmp (tmp_char, "define_constants"))
571 read_constants (infile, tmp_char);
575 fatal_with_file_and_line (infile, "unknown rtx code `%s'", tmp_char);
578 /* If we end up with an insn expression then we free this space below. */
579 return_rtx = rtx_alloc (tmp_code);
580 format_ptr = GET_RTX_FORMAT (GET_CODE (return_rtx));
582 /* If what follows is `: mode ', read it and
583 store the mode in the rtx. */
585 i = read_skip_spaces (infile);
588 read_name (tmp_char, infile);
589 for (j = 0; j < NUM_MACHINE_MODES; j++)
590 if (! strcmp (GET_MODE_NAME (j), tmp_char))
593 if (j == MAX_MACHINE_MODE)
594 fatal_with_file_and_line (infile, "unknown mode `%s'", tmp_char);
596 PUT_MODE (return_rtx, (enum machine_mode) j);
601 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (return_rtx)); i++)
602 switch (*format_ptr++)
604 /* 0 means a field for internal use only.
605 Don't expect it to be present in the input. */
611 XEXP (return_rtx, i) = read_rtx (infile);
615 /* 'V' is an optional vector: if a closeparen follows,
616 just store NULL for this element. */
617 c = read_skip_spaces (infile);
621 XVEC (return_rtx, i) = 0;
624 /* Now process the vector. */
628 /* Obstack to store scratch vector in. */
629 struct obstack vector_stack;
630 int list_counter = 0;
631 rtvec return_vec = NULL_RTVEC;
633 c = read_skip_spaces (infile);
635 fatal_expected_char (infile, '[', c);
637 /* Add expressions to a list, while keeping a count. */
638 obstack_init (&vector_stack);
639 while ((c = read_skip_spaces (infile)) && c != ']')
643 obstack_ptr_grow (&vector_stack, read_rtx (infile));
645 if (list_counter > 0)
647 return_vec = rtvec_alloc (list_counter);
648 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
649 list_counter * sizeof (rtx));
651 XVEC (return_rtx, i) = return_vec;
652 obstack_free (&vector_stack, NULL);
653 /* close bracket gotten */
664 c = read_skip_spaces (infile);
668 /* 'S' fields are optional and should be NULL if no string
669 was given. Also allow normal 's' and 'T' strings to be
670 omitted, treating them in the same way as empty strings. */
671 XSTR (return_rtx, i) = (format_ptr[-1] == 'S' ? NULL : "");
675 /* The output template slot of a DEFINE_INSN,
676 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
677 gets a star inserted as its first character, if it is
678 written with a brace block instead of a string constant. */
679 star_if_braced = (format_ptr[-1] == 'T');
681 stringbuf = read_string (infile, star_if_braced);
683 /* For insn patterns, we want to provide a default name
684 based on the file and line, like "*foo.md:12", if the
685 given name is blank. These are only for define_insn and
686 define_insn_and_split, to aid debugging. */
687 if (*stringbuf == '\0'
689 && (GET_CODE (return_rtx) == DEFINE_INSN
690 || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT))
693 const char *fn = (read_rtx_filename ? read_rtx_filename : "rtx");
695 for (slash = fn; *slash; slash ++)
696 if (*slash == '/' || *slash == '\\' || *slash == ':')
698 obstack_1grow (&string_obstack, '*');
699 obstack_grow (&string_obstack, fn, strlen (fn));
700 sprintf (line_name, ":%d", read_rtx_lineno);
701 obstack_grow (&string_obstack, line_name, strlen (line_name)+1);
702 stringbuf = (char *) obstack_finish (&string_obstack);
706 XTMPL (return_rtx, i) = stringbuf;
708 XSTR (return_rtx, i) = stringbuf;
713 read_name (tmp_char, infile);
714 validate_const_int (infile, tmp_char);
715 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
716 tmp_wide = atoi (tmp_char);
718 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
719 tmp_wide = atol (tmp_char);
721 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
722 But prefer not to use our hand-rolled function above either. */
723 #if defined(HAVE_ATOLL) || !defined(HAVE_ATOQ)
724 tmp_wide = atoll (tmp_char);
726 tmp_wide = atoq (tmp_char);
730 XWINT (return_rtx, i) = tmp_wide;
735 read_name (tmp_char, infile);
736 validate_const_int (infile, tmp_char);
737 tmp_int = atoi (tmp_char);
738 XINT (return_rtx, i) = tmp_int;
743 "switch format wrong in rtl.read_rtx(). format was: %c.\n",
745 fprintf (stderr, "\tfile position: %ld\n", ftell (infile));
749 c = read_skip_spaces (infile);
751 fatal_expected_char (infile, ')', c);