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 (FILE *infile
, const char *msg
, ...)
63 fprintf (stderr
, "%s:%d: ", read_rtx_filename
, read_rtx_lineno
);
64 vfprintf (stderr
, msg
, ap
);
67 /* Gather some following context. */
68 for (i
= 0; i
< sizeof (context
)-1; ++i
)
73 if (c
== '\r' || c
== '\n')
79 fprintf (stderr
, "%s:%d: following context is `%s'\n",
80 read_rtx_filename
, read_rtx_lineno
, context
);
86 /* Dump code after printing a message. Used when read_rtx finds
90 fatal_expected_char (infile
, expected_c
, actual_c
)
92 int expected_c
, actual_c
;
94 fatal_with_file_and_line (infile
, "expected character `%c', found `%c'",
95 expected_c
, actual_c
);
98 /* Read chars from INFILE until a non-whitespace char
99 and return that. Comments, both Lisp style and C style,
100 are treated as whitespace.
101 Tools such as genflags use this function. */
104 read_skip_spaces (infile
)
118 case ' ': case '\t': case '\f': case '\r':
124 while (c
!= '\n' && c
!= EOF
);
133 fatal_expected_char (infile
, '*', c
);
136 while ((c
= getc (infile
)) && c
!= EOF
)
140 else if (prevc
== '*' && c
== '/')
153 /* Read an rtx code name into the buffer STR[].
154 It is terminated by any of the punctuation chars of rtx printed syntax. */
157 read_name (str
, infile
)
164 c
= read_skip_spaces (infile
);
169 if (c
== ' ' || c
== '\n' || c
== '\t' || c
== '\f' || c
== '\r')
171 if (c
== ':' || c
== ')' || c
== ']' || c
== '"' || c
== '/'
172 || c
== '(' || c
== '[')
181 fatal_with_file_and_line (infile
, "missing name or number");
189 /* Do constant expansion. */
190 struct md_constant
*def
;
195 struct md_constant tmp_def
;
198 def
= htab_find (md_constants
, &tmp_def
);
207 /* Subroutine of the string readers. Handles backslash escapes.
208 Caller has read the backslash, but not placed it into the obstack. */
210 read_escape (ob
, infile
)
214 int c
= getc (infile
);
218 /* Backslash-newline is replaced by nothing, as in C. */
223 /* \" \' \\ are replaced by the second character. */
229 /* Standard C string escapes:
232 all are passed through to the output string unmolested.
233 In normal use these wind up in a string constant processed
234 by the C compiler, which will translate them appropriately.
235 We do not bother checking that \[0-7] are followed by up to
236 two octal digits, or that \x is followed by N hex digits.
237 \? \u \U are left out because they are not in traditional C. */
238 case 'a': case 'b': case 'f': case 'n': case 'r': case 't': case 'v':
239 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
241 obstack_1grow (ob
, '\\');
244 /* \; makes stuff for a C string constant containing
247 obstack_grow (ob
, "\\n\\t", 4);
250 /* pass anything else through, but issue a warning. */
252 fprintf (stderr
, "%s:%d: warning: unrecognized escape \\%c\n",
253 read_rtx_filename
, read_rtx_lineno
, c
);
254 obstack_1grow (ob
, '\\');
258 obstack_1grow (ob
, c
);
262 /* Read a double-quoted string onto the obstack. Caller has scanned
263 the leading quote. */
265 read_quoted_string (ob
, infile
)
273 c
= getc (infile
); /* Read the string */
278 read_escape (ob
, infile
);
284 obstack_1grow (ob
, c
);
287 obstack_1grow (ob
, 0);
288 return obstack_finish (ob
);
291 /* Read a braced string (a la Tcl) onto the obstack. Caller has
292 scanned the leading brace. Note that unlike quoted strings,
293 the outermost braces _are_ included in the string constant. */
295 read_braced_string (ob
, infile
)
300 int brace_depth
= 1; /* caller-processed */
301 unsigned long starting_read_rtx_lineno
= read_rtx_lineno
;
303 obstack_1grow (ob
, '{');
306 c
= getc (infile
); /* Read the string */
316 read_escape (ob
, infile
);
320 fatal_with_file_and_line
321 (infile
, "missing closing } for opening brace on line %lu",
322 starting_read_rtx_lineno
);
324 obstack_1grow (ob
, c
);
327 obstack_1grow (ob
, 0);
328 return obstack_finish (ob
);
331 /* Read some kind of string constant. This is the high-level routine
332 used by read_rtx. It handles surrounding parentheses, leading star,
333 and dispatch to the appropriate string constant reader. */
336 read_string (ob
, infile
, star_if_braced
)
345 c
= read_skip_spaces (infile
);
349 c
= read_skip_spaces (infile
);
353 stringbuf
= read_quoted_string (ob
, infile
);
357 obstack_1grow (ob
, '*');
358 stringbuf
= read_braced_string (ob
, infile
);
361 fatal_with_file_and_line (infile
, "expected `\"' or `{', found `%c'", c
);
365 c
= read_skip_spaces (infile
);
367 fatal_expected_char (infile
, ')', c
);
373 /* Provide a version of a function to read a long long if the system does
375 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !defined(HAVE_ATOLL) && !defined(HAVE_ATOQ)
376 HOST_WIDE_INT atoll
PARAMS ((const char *));
383 HOST_WIDE_INT tmp_wide
;
395 HOST_WIDE_INT new_wide
= tmp_wide
*10 + (*p
- '0');
396 if (new_wide
< tmp_wide
)
398 /* Return INT_MAX equiv on overflow. */
399 tmp_wide
= (~(unsigned HOST_WIDE_INT
) 0) >> 1;
407 tmp_wide
= -tmp_wide
;
412 /* Given a constant definition, return a hash code for its name. */
418 const char *string
= ((const struct md_constant
*) def
)->name
;
420 for (result
= i
= 0;*string
++ != '\0'; i
++)
421 result
+= ((unsigned char) *string
<< (i
% CHAR_BIT
));
425 /* Given two constant definitions, return true if they have the same name. */
427 def_name_eq_p (def1
, def2
)
428 const void *def1
, *def2
;
430 return ! strcmp (((const struct md_constant
*) def1
)->name
,
431 ((const struct md_constant
*) def2
)->name
);
434 /* INFILE is a FILE pointer to read text from. TMP_CHAR is a buffer suitable
435 to read a name or number into. Process a define_constants directive,
436 starting with the optional space after the "define_constants". */
438 read_constants (infile
, tmp_char
)
445 c
= read_skip_spaces (infile
);
447 fatal_expected_char (infile
, '[', c
);
450 defs
= htab_create (32, def_hash
, def_name_eq_p
, (htab_del
) 0);
451 /* Disable constant expansion during definition processing. */
453 while ( (c
= read_skip_spaces (infile
)) != ']')
455 struct md_constant
*def
;
459 fatal_expected_char (infile
, '(', c
);
460 def
= xmalloc (sizeof (struct md_constant
));
461 def
->name
= tmp_char
;
462 read_name (tmp_char
, infile
);
463 entry_ptr
= htab_find_slot (defs
, def
, TRUE
);
465 def
->name
= xstrdup (tmp_char
);
466 c
= read_skip_spaces (infile
);
468 read_name (tmp_char
, infile
);
471 def
->value
= xstrdup (tmp_char
);
477 if (strcmp (def
->value
, tmp_char
))
478 fatal_with_file_and_line (infile
,
479 "redefinition of %s, was %s, now %s",
480 def
->name
, def
->value
, tmp_char
);
482 c
= read_skip_spaces (infile
);
484 fatal_expected_char (infile
, ')', c
);
487 c
= read_skip_spaces (infile
);
489 fatal_expected_char (infile
, ')', c
);
492 /* For every constant definition, call CALLBACK with two arguments:
493 a pointer a pointer to the constant definition and INFO.
494 Stops when CALLBACK returns zero. */
496 traverse_md_constants (callback
, info
)
501 htab_traverse (md_constants
, callback
, info
);
505 validate_const_int (infile
, string
)
513 while (*cp
&& ISSPACE (*cp
))
515 if (*cp
== '-' || *cp
== '+')
523 fatal_with_file_and_line (infile
, "invalid decimal constant \"%s\"\n", string
);
526 /* Read an rtx in printed representation from INFILE
527 and return an actual rtx in core constructed accordingly.
528 read_rtx is not used in the compiler proper, but rather in
529 the utilities gen*.c that construct C code from machine descriptions. */
537 const char *format_ptr
;
538 /* tmp_char is a buffer used for reading decimal integers
539 and names of rtx types and machine modes.
540 Therefore, 256 must be enough. */
545 HOST_WIDE_INT tmp_wide
;
547 /* Obstack used for allocating RTL objects. */
548 static struct obstack rtl_obstack
;
549 static int initialized
;
551 /* Linked list structure for making RTXs: */
554 struct rtx_list
*next
;
555 rtx value
; /* Value of this node. */
559 obstack_init (&rtl_obstack
);
564 c
= read_skip_spaces (infile
); /* Should be open paren. */
566 fatal_expected_char (infile
, '(', c
);
568 read_name (tmp_char
, infile
);
572 if (! strcmp (tmp_char
, "define_constants"))
574 read_constants (infile
, tmp_char
);
577 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
578 if (! strcmp (tmp_char
, GET_RTX_NAME (i
)))
580 tmp_code
= (RTX_CODE
) i
; /* get value for name */
584 if (tmp_code
== UNKNOWN
)
585 fatal_with_file_and_line (infile
, "unknown rtx code `%s'", tmp_char
);
587 /* (NIL) stands for an expression that isn't there. */
590 /* Discard the closeparen. */
591 while ((c
= getc (infile
)) && c
!= ')')
597 /* If we end up with an insn expression then we free this space below. */
598 return_rtx
= rtx_alloc (tmp_code
);
599 format_ptr
= GET_RTX_FORMAT (GET_CODE (return_rtx
));
601 /* If what follows is `: mode ', read it and
602 store the mode in the rtx. */
604 i
= read_skip_spaces (infile
);
607 read_name (tmp_char
, infile
);
608 for (j
= 0; j
< NUM_MACHINE_MODES
; j
++)
609 if (! strcmp (GET_MODE_NAME (j
), tmp_char
))
612 if (j
== MAX_MACHINE_MODE
)
613 fatal_with_file_and_line (infile
, "unknown mode `%s'", tmp_char
);
615 PUT_MODE (return_rtx
, (enum machine_mode
) j
);
620 for (i
= 0; i
< GET_RTX_LENGTH (GET_CODE (return_rtx
)); i
++)
621 switch (*format_ptr
++)
623 /* 0 means a field for internal use only.
624 Don't expect it to be present in the input. */
630 XEXP (return_rtx
, i
) = read_rtx (infile
);
634 /* 'V' is an optional vector: if a closeparen follows,
635 just store NULL for this element. */
636 c
= read_skip_spaces (infile
);
640 XVEC (return_rtx
, i
) = 0;
643 /* Now process the vector. */
647 /* Obstack to store scratch vector in. */
648 struct obstack vector_stack
;
649 int list_counter
= 0;
650 rtvec return_vec
= NULL_RTVEC
;
652 c
= read_skip_spaces (infile
);
654 fatal_expected_char (infile
, '[', c
);
656 /* add expressions to a list, while keeping a count */
657 obstack_init (&vector_stack
);
658 while ((c
= read_skip_spaces (infile
)) && c
!= ']')
662 obstack_ptr_grow (&vector_stack
, read_rtx (infile
));
664 if (list_counter
> 0)
666 return_vec
= rtvec_alloc (list_counter
);
667 memcpy (&return_vec
->elem
[0], obstack_finish (&vector_stack
),
668 list_counter
* sizeof (rtx
));
670 XVEC (return_rtx
, i
) = return_vec
;
671 obstack_free (&vector_stack
, NULL
);
672 /* close bracket gotten */
677 /* 'S' is an optional string: if a closeparen follows,
678 just store NULL for this element. */
679 c
= read_skip_spaces (infile
);
683 XSTR (return_rtx
, i
) = 0;
692 /* The output template slot of a DEFINE_INSN,
693 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
694 gets a star inserted as its first character, if it is
695 written with a brace block instead of a string constant. */
696 int star_if_braced
= (format_ptr
[-1] == 'T');
698 stringbuf
= read_string (&rtl_obstack
, infile
, star_if_braced
);
700 /* For insn patterns, we want to provide a default name
701 based on the file and line, like "*foo.md:12", if the
702 given name is blank. These are only for define_insn and
703 define_insn_and_split, to aid debugging. */
704 if (*stringbuf
== '\0'
706 && (GET_CODE (return_rtx
) == DEFINE_INSN
707 || GET_CODE (return_rtx
) == DEFINE_INSN_AND_SPLIT
))
710 const char *fn
= (read_rtx_filename
? read_rtx_filename
: "rtx");
712 for (slash
= fn
; *slash
; slash
++)
713 if (*slash
== '/' || *slash
== '\\' || *slash
== ':')
715 obstack_1grow (&rtl_obstack
, '*');
716 obstack_grow (&rtl_obstack
, fn
, strlen (fn
));
717 sprintf (line_name
, ":%d", read_rtx_lineno
);
718 obstack_grow (&rtl_obstack
, line_name
, strlen (line_name
)+1);
719 stringbuf
= (char *) obstack_finish (&rtl_obstack
);
723 XTMPL (return_rtx
, i
) = stringbuf
;
725 XSTR (return_rtx
, i
) = stringbuf
;
730 read_name (tmp_char
, infile
);
731 validate_const_int (infile
, tmp_char
);
732 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
733 tmp_wide
= atoi (tmp_char
);
735 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
736 tmp_wide
= atol (tmp_char
);
738 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
739 But prefer not to use our hand-rolled function above either. */
740 #if defined(HAVE_ATOLL) || !defined(HAVE_ATOQ)
741 tmp_wide
= atoll (tmp_char
);
743 tmp_wide
= atoq (tmp_char
);
747 XWINT (return_rtx
, i
) = tmp_wide
;
752 read_name (tmp_char
, infile
);
753 validate_const_int (infile
, tmp_char
);
754 tmp_int
= atoi (tmp_char
);
755 XINT (return_rtx
, i
) = tmp_int
;
760 "switch format wrong in rtl.read_rtx(). format was: %c.\n",
762 fprintf (stderr
, "\tfile position: %ld\n", ftell (infile
));
766 c
= read_skip_spaces (infile
);
768 fatal_expected_char (infile
, ')', c
);