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
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
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
24 #include "coretypes.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>";
54 fatal_with_file_and_line VPARAMS ((FILE *infile, const char *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);
68 /* Gather some following context. */
69 for (i = 0; i < sizeof (context)-1; ++i)
74 if (c == '\r' || c == '\n')
80 fprintf (stderr, "%s:%d: following context is `%s'\n",
81 read_rtx_filename, read_rtx_lineno, context);
87 /* Dump code after printing a message. Used when read_rtx finds
91 fatal_expected_char (infile, expected_c, actual_c)
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)
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 (str, infile)
165 c = read_skip_spaces (infile);
170 if (c == ' ' || c == '\n' || c == '\t' || c == '\f' || c == '\r')
172 if (c == ':' || c == ')' || c == ']' || c == '"' || c == '/'
173 || c == '(' || c == '[')
182 fatal_with_file_and_line (infile, "missing name or number");
190 /* Do constant expansion. */
191 struct md_constant *def;
196 struct md_constant tmp_def;
199 def = htab_find (md_constants, &tmp_def);
208 /* Subroutine of the string readers. Handles backslash escapes.
209 Caller has read the backslash, but not placed it into the obstack. */
211 read_escape (ob, infile)
215 int c = getc (infile);
219 /* Backslash-newline is replaced by nothing, as in C. */
224 /* \" \' \\ are replaced by the second character. */
230 /* Standard C string escapes:
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':
242 obstack_1grow (ob, '\\');
245 /* \; makes stuff for a C string constant containing
248 obstack_grow (ob, "\\n\\t", 4);
251 /* pass anything else through, but issue a warning. */
253 fprintf (stderr, "%s:%d: warning: unrecognized escape \\%c\n",
254 read_rtx_filename, read_rtx_lineno, c);
255 obstack_1grow (ob, '\\');
259 obstack_1grow (ob, c);
263 /* Read a double-quoted string onto the obstack. Caller has scanned
264 the leading quote. */
266 read_quoted_string (ob, infile)
274 c = getc (infile); /* Read the string */
279 read_escape (ob, infile);
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. */
296 read_braced_string (ob, infile)
301 int brace_depth = 1; /* caller-processed */
303 obstack_1grow (ob, '{');
306 c = getc (infile); /* Read the string */
315 read_escape (ob, infile);
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. */
331 read_string (ob, infile, star_if_braced)
340 c = read_skip_spaces (infile);
344 c = read_skip_spaces (infile);
348 stringbuf = read_quoted_string (ob, infile);
352 obstack_1grow (ob, '*');
353 stringbuf = read_braced_string (ob, infile);
356 fatal_with_file_and_line (infile, "expected `\"' or `{', found `%c'", c);
360 c = read_skip_spaces (infile);
362 fatal_expected_char (infile, ')', c);
368 /* Provide a version of a function to read a long long if the system does
370 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !defined(HAVE_ATOLL) && !defined(HAVE_ATOQ)
376 HOST_WIDE_INT tmp_wide;
388 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
389 if (new_wide < tmp_wide)
391 /* Return INT_MAX equiv on overflow. */
392 tmp_wide = (~(unsigned HOST_WIDE_INT) 0) >> 1;
400 tmp_wide = -tmp_wide;
405 /* Given a constant definition, return a hash code for its name. */
411 const char *string = ((const struct md_constant *) def)->name;
413 for (result = i = 0;*string++ != '\0'; i++)
414 result += ((unsigned char) *string << (i % CHAR_BIT));
418 /* Given two constant definitions, return true if they have the same name. */
420 def_name_eq_p (def1, def2)
421 const void *def1, *def2;
423 return ! strcmp (((const struct md_constant *) def1)->name,
424 ((const struct md_constant *) def2)->name);
427 /* INFILE is a FILE pointer to read text from. TMP_CHAR is a buffer suitable
428 to read a name or number into. Process a define_constants directive,
429 starting with the optional space after the "define_constants". */
431 read_constants (infile, tmp_char)
438 c = read_skip_spaces (infile);
440 fatal_expected_char (infile, '[', c);
443 defs = htab_create (32, def_hash, def_name_eq_p, (htab_del) 0);
444 /* Disable constant expansion during definition processing. */
446 while ( (c = read_skip_spaces (infile)) != ']')
448 struct md_constant *def;
452 fatal_expected_char (infile, '(', c);
453 def = xmalloc (sizeof (struct md_constant));
454 def->name = tmp_char;
455 read_name (tmp_char, infile);
456 entry_ptr = htab_find_slot (defs, def, TRUE);
458 def->name = xstrdup (tmp_char);
459 c = read_skip_spaces (infile);
461 read_name (tmp_char, infile);
464 def->value = xstrdup (tmp_char);
470 if (strcmp (def->value, tmp_char))
471 fatal_with_file_and_line (infile,
472 "redefinition of %s, was %s, now %s",
473 def->name, def->value, tmp_char);
475 c = read_skip_spaces (infile);
477 fatal_expected_char (infile, ')', c);
480 c = read_skip_spaces (infile);
482 fatal_expected_char (infile, ')', c);
485 /* For every constant definition, call CALLBACK with two arguments:
486 a pointer a pointer to the constant definition and INFO.
487 Stops when CALLBACK returns zero. */
489 traverse_md_constants (callback, info)
494 htab_traverse (md_constants, callback, info);
498 validate_const_int (infile, string)
506 while (*cp && ISSPACE (*cp))
508 if (*cp == '-' || *cp == '+')
516 fatal_with_file_and_line (infile, "invalid decimal constant \"%s\"\n", string);
519 /* Read an rtx in printed representation from INFILE
520 and return an actual rtx in core constructed accordingly.
521 read_rtx is not used in the compiler proper, but rather in
522 the utilities gen*.c that construct C code from machine descriptions. */
530 const char *format_ptr;
531 /* tmp_char is a buffer used for reading decimal integers
532 and names of rtx types and machine modes.
533 Therefore, 256 must be enough. */
538 HOST_WIDE_INT tmp_wide;
540 /* Obstack used for allocating RTL objects. */
541 static struct obstack rtl_obstack;
542 static int initialized;
544 /* Linked list structure for making RTXs: */
547 struct rtx_list *next;
548 rtx value; /* Value of this node. */
552 obstack_init (&rtl_obstack);
557 c = read_skip_spaces (infile); /* Should be open paren. */
559 fatal_expected_char (infile, '(', c);
561 read_name (tmp_char, infile);
565 if (! strcmp (tmp_char, "define_constants"))
567 read_constants (infile, tmp_char);
570 for (i = 0; i < NUM_RTX_CODE; i++)
571 if (! strcmp (tmp_char, GET_RTX_NAME (i)))
573 tmp_code = (RTX_CODE) i; /* get value for name */
577 if (tmp_code == UNKNOWN)
578 fatal_with_file_and_line (infile, "unknown rtx code `%s'", tmp_char);
580 /* (NIL) stands for an expression that isn't there. */
583 /* Discard the closeparen. */
584 while ((c = getc (infile)) && c != ')')
590 /* If we end up with an insn expression then we free this space below. */
591 return_rtx = rtx_alloc (tmp_code);
592 format_ptr = GET_RTX_FORMAT (GET_CODE (return_rtx));
594 /* If what follows is `: mode ', read it and
595 store the mode in the rtx. */
597 i = read_skip_spaces (infile);
600 read_name (tmp_char, infile);
601 for (j = 0; j < NUM_MACHINE_MODES; j++)
602 if (! strcmp (GET_MODE_NAME (j), tmp_char))
605 if (j == MAX_MACHINE_MODE)
606 fatal_with_file_and_line (infile, "unknown mode `%s'", tmp_char);
608 PUT_MODE (return_rtx, (enum machine_mode) j);
613 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (return_rtx)); i++)
614 switch (*format_ptr++)
616 /* 0 means a field for internal use only.
617 Don't expect it to be present in the input. */
623 XEXP (return_rtx, i) = read_rtx (infile);
627 /* 'V' is an optional vector: if a closeparen follows,
628 just store NULL for this element. */
629 c = read_skip_spaces (infile);
633 XVEC (return_rtx, i) = 0;
636 /* Now process the vector. */
640 /* Obstack to store scratch vector in. */
641 struct obstack vector_stack;
642 int list_counter = 0;
643 rtvec return_vec = NULL_RTVEC;
645 c = read_skip_spaces (infile);
647 fatal_expected_char (infile, '[', c);
649 /* add expressions to a list, while keeping a count */
650 obstack_init (&vector_stack);
651 while ((c = read_skip_spaces (infile)) && c != ']')
655 obstack_ptr_grow (&vector_stack, (PTR) read_rtx (infile));
657 if (list_counter > 0)
659 return_vec = rtvec_alloc (list_counter);
660 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
661 list_counter * sizeof (rtx));
663 XVEC (return_rtx, i) = return_vec;
664 obstack_free (&vector_stack, NULL);
665 /* close bracket gotten */
670 /* 'S' is an optional string: if a closeparen follows,
671 just store NULL for this element. */
672 c = read_skip_spaces (infile);
676 XSTR (return_rtx, i) = 0;
685 /* The output template slot of a DEFINE_INSN,
686 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
687 gets a star inserted as its first character, if it is
688 written with a brace block instead of a string constant. */
689 int star_if_braced = (format_ptr[-1] == 'T');
691 stringbuf = read_string (&rtl_obstack, infile, star_if_braced);
693 /* For insn patterns, we want to provide a default name
694 based on the file and line, like "*foo.md:12", if the
695 given name is blank. These are only for define_insn and
696 define_insn_and_split, to aid debugging. */
697 if (*stringbuf == '\0'
699 && (GET_CODE (return_rtx) == DEFINE_INSN
700 || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT))
703 const char *fn = (read_rtx_filename ? read_rtx_filename : "rtx");
705 for (slash = fn; *slash; slash ++)
706 if (*slash == '/' || *slash == '\\' || *slash == ':')
708 obstack_1grow (&rtl_obstack, '*');
709 obstack_grow (&rtl_obstack, fn, strlen (fn));
710 sprintf (line_name, ":%d", read_rtx_lineno);
711 obstack_grow (&rtl_obstack, line_name, strlen (line_name)+1);
712 stringbuf = (char *) obstack_finish (&rtl_obstack);
716 XTMPL (return_rtx, i) = stringbuf;
718 XSTR (return_rtx, i) = stringbuf;
723 read_name (tmp_char, infile);
724 validate_const_int (infile, tmp_char);
725 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
726 tmp_wide = atoi (tmp_char);
728 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
729 tmp_wide = atol (tmp_char);
731 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
732 But prefer not to use our hand-rolled function above either. */
733 #if defined(HAVE_ATOLL) || !defined(HAVE_ATOQ)
734 tmp_wide = atoll (tmp_char);
736 tmp_wide = atoq (tmp_char);
740 XWINT (return_rtx, i) = tmp_wide;
745 read_name (tmp_char, infile);
746 validate_const_int (infile, tmp_char);
747 tmp_int = atoi (tmp_char);
748 XINT (return_rtx, i) = tmp_int;
753 "switch format wrong in rtl.read_rtx(). format was: %c.\n",
755 fprintf (stderr, "\tfile position: %ld\n", ftell (infile));
759 c = read_skip_spaces (infile);
761 fatal_expected_char (infile, ')', c);