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 (struct obstack *, FILE *, int);
38 static char *read_quoted_string (struct obstack *, FILE *);
39 static char *read_braced_string (struct obstack *, FILE *);
40 static void read_escape (struct obstack *, 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 /* 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>";
55 fatal_with_file_and_line (FILE *infile, 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 (FILE *infile, int expected_c, int actual_c)
93 fatal_with_file_and_line (infile, "expected character `%c', found `%c'",
94 expected_c, actual_c);
97 /* Read chars from INFILE until a non-whitespace char
98 and return that. Comments, both Lisp style and C style,
99 are treated as whitespace.
100 Tools such as genflags use this function. */
103 read_skip_spaces (FILE *infile)
116 case ' ': case '\t': case '\f': case '\r':
122 while (c != '\n' && c != EOF);
131 fatal_expected_char (infile, '*', c);
134 while ((c = getc (infile)) && c != EOF)
138 else if (prevc == '*' && c == '/')
151 /* Read an rtx code name into the buffer STR[].
152 It is terminated by any of the punctuation chars of rtx printed syntax. */
155 read_name (char *str, FILE *infile)
160 c = read_skip_spaces (infile);
165 if (c == ' ' || c == '\n' || c == '\t' || c == '\f' || c == '\r')
167 if (c == ':' || c == ')' || c == ']' || c == '"' || c == '/'
168 || c == '(' || c == '[')
177 fatal_with_file_and_line (infile, "missing name or number");
185 /* Do constant expansion. */
186 struct md_constant *def;
191 struct md_constant tmp_def;
194 def = htab_find (md_constants, &tmp_def);
203 /* Subroutine of the string readers. Handles backslash escapes.
204 Caller has read the backslash, but not placed it into the obstack. */
206 read_escape (struct obstack *ob, FILE *infile)
208 int c = getc (infile);
212 /* Backslash-newline is replaced by nothing, as in C. */
217 /* \" \' \\ are replaced by the second character. */
223 /* Standard C string escapes:
226 all are passed through to the output string unmolested.
227 In normal use these wind up in a string constant processed
228 by the C compiler, which will translate them appropriately.
229 We do not bother checking that \[0-7] are followed by up to
230 two octal digits, or that \x is followed by N hex digits.
231 \? \u \U are left out because they are not in traditional C. */
232 case 'a': case 'b': case 'f': case 'n': case 'r': case 't': case 'v':
233 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
235 obstack_1grow (ob, '\\');
238 /* \; makes stuff for a C string constant containing
241 obstack_grow (ob, "\\n\\t", 4);
244 /* pass anything else through, but issue a warning. */
246 fprintf (stderr, "%s:%d: warning: unrecognized escape \\%c\n",
247 read_rtx_filename, read_rtx_lineno, c);
248 obstack_1grow (ob, '\\');
252 obstack_1grow (ob, c);
256 /* Read a double-quoted string onto the obstack. Caller has scanned
257 the leading quote. */
259 read_quoted_string (struct obstack *ob, FILE *infile)
265 c = getc (infile); /* Read the string */
270 read_escape (ob, infile);
276 obstack_1grow (ob, c);
279 obstack_1grow (ob, 0);
280 return obstack_finish (ob);
283 /* Read a braced string (a la Tcl) onto the obstack. Caller has
284 scanned the leading brace. Note that unlike quoted strings,
285 the outermost braces _are_ included in the string constant. */
287 read_braced_string (struct obstack *ob, FILE *infile)
290 int brace_depth = 1; /* caller-processed */
291 unsigned long starting_read_rtx_lineno = read_rtx_lineno;
293 obstack_1grow (ob, '{');
296 c = getc (infile); /* Read the string */
306 read_escape (ob, infile);
310 fatal_with_file_and_line
311 (infile, "missing closing } for opening brace on line %lu",
312 starting_read_rtx_lineno);
314 obstack_1grow (ob, c);
317 obstack_1grow (ob, 0);
318 return obstack_finish (ob);
321 /* Read some kind of string constant. This is the high-level routine
322 used by read_rtx. It handles surrounding parentheses, leading star,
323 and dispatch to the appropriate string constant reader. */
326 read_string (struct obstack *ob, FILE *infile, int star_if_braced)
332 c = read_skip_spaces (infile);
336 c = read_skip_spaces (infile);
340 stringbuf = read_quoted_string (ob, infile);
344 obstack_1grow (ob, '*');
345 stringbuf = read_braced_string (ob, infile);
348 fatal_with_file_and_line (infile, "expected `\"' or `{', found `%c'", c);
352 c = read_skip_spaces (infile);
354 fatal_expected_char (infile, ')', c);
360 /* Provide a version of a function to read a long long if the system does
362 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !defined(HAVE_ATOLL) && !defined(HAVE_ATOQ)
363 HOST_WIDE_INT atoll (const char *);
366 atoll (const char *p)
369 HOST_WIDE_INT tmp_wide;
381 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
382 if (new_wide < tmp_wide)
384 /* Return INT_MAX equiv on overflow. */
385 tmp_wide = (~(unsigned HOST_WIDE_INT) 0) >> 1;
393 tmp_wide = -tmp_wide;
398 /* Given a constant definition, return a hash code for its name. */
400 def_hash (const void *def)
403 const char *string = ((const struct md_constant *) def)->name;
405 for (result = i = 0;*string++ != '\0'; i++)
406 result += ((unsigned char) *string << (i % CHAR_BIT));
410 /* Given two constant definitions, return true if they have the same name. */
412 def_name_eq_p (const void *def1, const void *def2)
414 return ! strcmp (((const struct md_constant *) def1)->name,
415 ((const struct md_constant *) def2)->name);
418 /* INFILE is a FILE pointer to read text from. TMP_CHAR is a buffer suitable
419 to read a name or number into. Process a define_constants directive,
420 starting with the optional space after the "define_constants". */
422 read_constants (FILE *infile, char *tmp_char)
427 c = read_skip_spaces (infile);
429 fatal_expected_char (infile, '[', c);
432 defs = htab_create (32, def_hash, def_name_eq_p, (htab_del) 0);
433 /* Disable constant expansion during definition processing. */
435 while ( (c = read_skip_spaces (infile)) != ']')
437 struct md_constant *def;
441 fatal_expected_char (infile, '(', c);
442 def = xmalloc (sizeof (struct md_constant));
443 def->name = tmp_char;
444 read_name (tmp_char, infile);
445 entry_ptr = htab_find_slot (defs, def, TRUE);
447 def->name = xstrdup (tmp_char);
448 c = read_skip_spaces (infile);
450 read_name (tmp_char, infile);
453 def->value = xstrdup (tmp_char);
459 if (strcmp (def->value, tmp_char))
460 fatal_with_file_and_line (infile,
461 "redefinition of %s, was %s, now %s",
462 def->name, def->value, tmp_char);
464 c = read_skip_spaces (infile);
466 fatal_expected_char (infile, ')', c);
469 c = read_skip_spaces (infile);
471 fatal_expected_char (infile, ')', c);
474 /* For every constant definition, call CALLBACK with two arguments:
475 a pointer a pointer to the constant definition and INFO.
476 Stops when CALLBACK returns zero. */
478 traverse_md_constants (htab_trav callback, void *info)
481 htab_traverse (md_constants, callback, info);
485 validate_const_int (FILE *infile, const char *string)
491 while (*cp && ISSPACE (*cp))
493 if (*cp == '-' || *cp == '+')
501 fatal_with_file_and_line (infile, "invalid decimal constant \"%s\"\n", string);
504 /* Read an rtx in printed representation from INFILE
505 and return an actual rtx in core constructed accordingly.
506 read_rtx is not used in the compiler proper, but rather in
507 the utilities gen*.c that construct C code from machine descriptions. */
510 read_rtx (FILE *infile)
514 const char *format_ptr;
515 /* tmp_char is a buffer used for reading decimal integers
516 and names of rtx types and machine modes.
517 Therefore, 256 must be enough. */
522 HOST_WIDE_INT tmp_wide;
524 /* Obstack used for allocating RTL objects. */
525 static struct obstack rtl_obstack;
526 static int initialized;
528 /* Linked list structure for making RTXs: */
531 struct rtx_list *next;
532 rtx value; /* Value of this node. */
536 obstack_init (&rtl_obstack);
541 c = read_skip_spaces (infile); /* Should be open paren. */
543 fatal_expected_char (infile, '(', c);
545 read_name (tmp_char, infile);
549 if (! strcmp (tmp_char, "define_constants"))
551 read_constants (infile, tmp_char);
554 for (i = 0; i < NUM_RTX_CODE; i++)
555 if (! strcmp (tmp_char, GET_RTX_NAME (i)))
557 tmp_code = (RTX_CODE) i; /* get value for name */
561 if (tmp_code == UNKNOWN)
562 fatal_with_file_and_line (infile, "unknown rtx code `%s'", tmp_char);
564 /* (NIL) stands for an expression that isn't there. */
567 /* Discard the closeparen. */
568 while ((c = getc (infile)) && c != ')')
574 /* If we end up with an insn expression then we free this space below. */
575 return_rtx = rtx_alloc (tmp_code);
576 format_ptr = GET_RTX_FORMAT (GET_CODE (return_rtx));
578 /* If what follows is `: mode ', read it and
579 store the mode in the rtx. */
581 i = read_skip_spaces (infile);
584 read_name (tmp_char, infile);
585 for (j = 0; j < NUM_MACHINE_MODES; j++)
586 if (! strcmp (GET_MODE_NAME (j), tmp_char))
589 if (j == MAX_MACHINE_MODE)
590 fatal_with_file_and_line (infile, "unknown mode `%s'", tmp_char);
592 PUT_MODE (return_rtx, (enum machine_mode) j);
597 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (return_rtx)); i++)
598 switch (*format_ptr++)
600 /* 0 means a field for internal use only.
601 Don't expect it to be present in the input. */
607 XEXP (return_rtx, i) = read_rtx (infile);
611 /* 'V' is an optional vector: if a closeparen follows,
612 just store NULL for this element. */
613 c = read_skip_spaces (infile);
617 XVEC (return_rtx, i) = 0;
620 /* Now process the vector. */
624 /* Obstack to store scratch vector in. */
625 struct obstack vector_stack;
626 int list_counter = 0;
627 rtvec return_vec = NULL_RTVEC;
629 c = read_skip_spaces (infile);
631 fatal_expected_char (infile, '[', c);
633 /* add expressions to a list, while keeping a count */
634 obstack_init (&vector_stack);
635 while ((c = read_skip_spaces (infile)) && c != ']')
639 obstack_ptr_grow (&vector_stack, read_rtx (infile));
641 if (list_counter > 0)
643 return_vec = rtvec_alloc (list_counter);
644 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
645 list_counter * sizeof (rtx));
647 XVEC (return_rtx, i) = return_vec;
648 obstack_free (&vector_stack, NULL);
649 /* close bracket gotten */
654 /* 'S' is an optional string: if a closeparen follows,
655 just store NULL for this element. */
656 c = read_skip_spaces (infile);
660 XSTR (return_rtx, i) = 0;
669 /* The output template slot of a DEFINE_INSN,
670 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
671 gets a star inserted as its first character, if it is
672 written with a brace block instead of a string constant. */
673 int star_if_braced = (format_ptr[-1] == 'T');
675 stringbuf = read_string (&rtl_obstack, infile, star_if_braced);
677 /* For insn patterns, we want to provide a default name
678 based on the file and line, like "*foo.md:12", if the
679 given name is blank. These are only for define_insn and
680 define_insn_and_split, to aid debugging. */
681 if (*stringbuf == '\0'
683 && (GET_CODE (return_rtx) == DEFINE_INSN
684 || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT))
687 const char *fn = (read_rtx_filename ? read_rtx_filename : "rtx");
689 for (slash = fn; *slash; slash ++)
690 if (*slash == '/' || *slash == '\\' || *slash == ':')
692 obstack_1grow (&rtl_obstack, '*');
693 obstack_grow (&rtl_obstack, fn, strlen (fn));
694 sprintf (line_name, ":%d", read_rtx_lineno);
695 obstack_grow (&rtl_obstack, line_name, strlen (line_name)+1);
696 stringbuf = (char *) obstack_finish (&rtl_obstack);
700 XTMPL (return_rtx, i) = stringbuf;
702 XSTR (return_rtx, i) = stringbuf;
707 read_name (tmp_char, infile);
708 validate_const_int (infile, tmp_char);
709 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
710 tmp_wide = atoi (tmp_char);
712 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
713 tmp_wide = atol (tmp_char);
715 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
716 But prefer not to use our hand-rolled function above either. */
717 #if defined(HAVE_ATOLL) || !defined(HAVE_ATOQ)
718 tmp_wide = atoll (tmp_char);
720 tmp_wide = atoq (tmp_char);
724 XWINT (return_rtx, i) = tmp_wide;
729 read_name (tmp_char, infile);
730 validate_const_int (infile, tmp_char);
731 tmp_int = atoi (tmp_char);
732 XINT (return_rtx, i) = tmp_int;
737 "switch format wrong in rtl.read_rtx(). format was: %c.\n",
739 fprintf (stderr, "\tfile position: %ld\n", ftell (infile));
743 c = read_skip_spaces (infile);
745 fatal_expected_char (infile, ')', c);