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 /* Disable rtl checking; it conflicts with the macro handling. */
26 #undef ENABLE_RTL_CHECKING
29 #include "coretypes.h"
35 static htab_t md_constants
;
37 /* One element in a singly-linked list of (integer, string) pairs. */
39 struct map_value
*next
;
44 /* Maps a macro or attribute name to a list of (integer, string) pairs.
45 The integers are mode or code values; the strings are either C conditions
46 or attribute values. */
48 /* The name of the macro or attribute. */
51 /* The group (modes or codes) to which the macro or attribute belongs. */
52 struct macro_group
*group
;
54 /* Gives a unique number to the attribute or macro. Numbers are
55 allocated consecutively, starting at 0. */
58 /* The list of (integer, string) pairs. */
59 struct map_value
*values
;
62 /* A structure for abstracting the common parts of code and mode macros. */
64 /* Tables of "mapping" structures, one for attributes and one for macros. */
67 /* The number of "real" modes or codes (and by extension, the first
68 number available for use as a macro placeholder). */
71 /* Treat the given string as the name of a standard mode or code and
72 return its integer value. Use the given file for error reporting. */
73 int (*find_builtin
) (const char *, FILE *);
75 /* Return true if the given rtx uses the given mode or code. */
76 bool (*uses_macro_p
) (rtx
, int);
78 /* Make the given rtx use the given mode or code. */
79 void (*apply_macro
) (rtx
, int);
82 /* Associates PTR (which can be a string, etc.) with the file location
83 specified by FILENAME and LINENO. */
90 /* If CODE is the number of a code macro, return a real rtx code that
91 has the same format. Return CODE otherwise. */
92 #define BELLWETHER_CODE(CODE) \
93 ((CODE) < NUM_RTX_CODE ? CODE : bellwether_codes[CODE - NUM_RTX_CODE])
95 static void fatal_with_file_and_line (FILE *, const char *, ...)
96 ATTRIBUTE_PRINTF_2 ATTRIBUTE_NORETURN
;
97 static void fatal_expected_char (FILE *, int, int) ATTRIBUTE_NORETURN
;
98 static int find_mode (const char *, FILE *);
99 static bool uses_mode_macro_p (rtx
, int);
100 static void apply_mode_macro (rtx
, int);
101 static int find_code (const char *, FILE *);
102 static bool uses_code_macro_p (rtx
, int);
103 static void apply_code_macro (rtx
, int);
104 static const char *apply_macro_to_string (const char *, struct mapping
*, int);
105 static rtx
apply_macro_to_rtx (rtx
, struct mapping
*, int);
106 static bool uses_macro_p (rtx
, struct mapping
*);
107 static const char *add_condition_to_string (const char *, const char *);
108 static void add_condition_to_rtx (rtx
, const char *);
109 static int apply_macro_traverse (void **, void *);
110 static struct mapping
*add_mapping (struct macro_group
*, htab_t t
,
111 const char *, FILE *);
112 static struct map_value
**add_map_value (struct map_value
**,
114 static void initialize_macros (void);
115 static void read_name (char *, FILE *);
116 static hashval_t
leading_ptr_hash (const void *);
117 static int leading_ptr_eq_p (const void *, const void *);
118 static void set_rtx_ptr_loc (const void *, const char *, int);
119 static const struct ptr_loc
*get_rtx_ptr_loc (const void *);
120 static char *read_string (FILE *, int);
121 static char *read_quoted_string (FILE *);
122 static char *read_braced_string (FILE *);
123 static void read_escape (FILE *);
124 static hashval_t
def_hash (const void *);
125 static int def_name_eq_p (const void *, const void *);
126 static void read_constants (FILE *infile
, char *tmp_char
);
127 static void validate_const_int (FILE *, const char *);
128 static int find_macro (struct macro_group
*, const char *, FILE *);
129 static struct mapping
*read_mapping (struct macro_group
*, htab_t
, FILE *);
130 static void check_code_macro (struct mapping
*, FILE *);
131 static rtx
read_rtx_1 (FILE *);
133 /* The mode and code macro structures. */
134 static struct macro_group modes
, codes
;
136 /* Index I is the value of BELLWETHER_CODE (I + NUM_RTX_CODE). */
137 static enum rtx_code
*bellwether_codes
;
139 /* Obstack used for allocating RTL strings. */
140 static struct obstack string_obstack
;
142 /* A table of ptr_locs, hashed on the PTR field. */
143 static htab_t ptr_locs
;
145 /* An obstack for the above. Plain xmalloc is a bit heavyweight for a
146 small structure like ptr_loc. */
147 static struct obstack ptr_loc_obstack
;
149 /* A hash table of triples (A, B, C), where each of A, B and C is a condition
150 and A is equivalent to "B && C". This is used to keep track of the source
151 of conditions that are made up of separate rtx strings (such as the split
152 condition of a define_insn_and_split). */
153 static htab_t joined_conditions
;
155 /* An obstack for allocating joined_conditions entries. */
156 static struct obstack joined_conditions_obstack
;
158 /* Subroutines of read_rtx. */
160 /* The current line number for the file. */
161 int read_rtx_lineno
= 1;
163 /* The filename for aborting with file and line. */
164 const char *read_rtx_filename
= "<unknown>";
167 fatal_with_file_and_line (FILE *infile
, const char *msg
, ...)
176 fprintf (stderr
, "%s:%d: ", read_rtx_filename
, read_rtx_lineno
);
177 vfprintf (stderr
, msg
, ap
);
180 /* Gather some following context. */
181 for (i
= 0; i
< sizeof (context
)-1; ++i
)
186 if (c
== '\r' || c
== '\n')
192 fprintf (stderr
, "%s:%d: following context is `%s'\n",
193 read_rtx_filename
, read_rtx_lineno
, context
);
199 /* Dump code after printing a message. Used when read_rtx finds
203 fatal_expected_char (FILE *infile
, int expected_c
, int actual_c
)
205 fatal_with_file_and_line (infile
, "expected character `%c', found `%c'",
206 expected_c
, actual_c
);
209 /* Implementations of the macro_group callbacks for modes. */
212 find_mode (const char *name
, FILE *infile
)
216 for (i
= 0; i
< NUM_MACHINE_MODES
; i
++)
217 if (strcmp (GET_MODE_NAME (i
), name
) == 0)
220 fatal_with_file_and_line (infile
, "unknown mode `%s'", name
);
224 uses_mode_macro_p (rtx x
, int mode
)
226 return (int) GET_MODE (x
) == mode
;
230 apply_mode_macro (rtx x
, int mode
)
235 /* Implementations of the macro_group callbacks for codes. */
238 find_code (const char *name
, FILE *infile
)
242 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
243 if (strcmp (GET_RTX_NAME (i
), name
) == 0)
246 fatal_with_file_and_line (infile
, "unknown rtx code `%s'", name
);
250 uses_code_macro_p (rtx x
, int code
)
252 return (int) GET_CODE (x
) == code
;
256 apply_code_macro (rtx x
, int code
)
261 /* Given that MACRO is being expanded as VALUE, apply the appropriate
262 string substitutions to STRING. Return the new string if any changes
263 were needed, otherwise return STRING itself. */
266 apply_macro_to_string (const char *string
, struct mapping
*macro
, int value
)
268 char *base
, *copy
, *p
, *attr
, *start
, *end
;
275 base
= p
= copy
= ASTRDUP (string
);
276 while ((start
= strchr (p
, '<')) && (end
= strchr (start
, '>')))
280 /* If there's a "macro:" prefix, check whether the macro name matches.
281 Set ATTR to the start of the attribute name. */
282 attr
= strchr (p
, ':');
283 if (attr
== 0 || attr
> end
)
287 if (strncmp (p
, macro
->name
, attr
- p
) != 0
288 || macro
->name
[attr
- p
] != 0)
293 /* Find the attribute specification. */
295 m
= (struct mapping
*) htab_find (macro
->group
->attrs
, &attr
);
300 /* Find the attribute value for VALUE. */
301 for (v
= m
->values
; v
!= 0; v
= v
->next
)
302 if (v
->number
== value
)
307 /* Add everything between the last copied byte and the '<',
308 then add in the attribute value. */
309 obstack_grow (&string_obstack
, base
, start
- base
);
310 obstack_grow (&string_obstack
, v
->string
, strlen (v
->string
));
315 obstack_grow (&string_obstack
, base
, strlen (base
) + 1);
316 copy
= obstack_finish (&string_obstack
);
317 copy_rtx_ptr_loc (copy
, string
);
323 /* Return a copy of ORIGINAL in which all uses of MACRO have been
324 replaced by VALUE. */
327 apply_macro_to_rtx (rtx original
, struct mapping
*macro
, int value
)
329 struct macro_group
*group
;
330 const char *format_ptr
;
333 enum rtx_code bellwether_code
;
338 /* Create a shallow copy of ORIGINAL. */
339 bellwether_code
= BELLWETHER_CODE (GET_CODE (original
));
340 x
= rtx_alloc (bellwether_code
);
341 memcpy (x
, original
, RTX_SIZE (bellwether_code
));
343 /* Change the mode or code itself. */
344 group
= macro
->group
;
345 if (group
->uses_macro_p (x
, macro
->index
+ group
->num_builtins
))
346 group
->apply_macro (x
, value
);
348 /* Change each string and recursively change each rtx. */
349 format_ptr
= GET_RTX_FORMAT (bellwether_code
);
350 for (i
= 0; format_ptr
[i
] != 0; i
++)
351 switch (format_ptr
[i
])
354 XTMPL (x
, i
) = apply_macro_to_string (XTMPL (x
, i
), macro
, value
);
359 XSTR (x
, i
) = apply_macro_to_string (XSTR (x
, i
), macro
, value
);
363 XEXP (x
, i
) = apply_macro_to_rtx (XEXP (x
, i
), macro
, value
);
368 if (XVEC (original
, i
))
370 XVEC (x
, i
) = rtvec_alloc (XVECLEN (original
, i
));
371 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
372 XVECEXP (x
, i
, j
) = apply_macro_to_rtx (XVECEXP (original
, i
, j
),
383 /* Return true if X (or some subexpression of X) uses macro MACRO. */
386 uses_macro_p (rtx x
, struct mapping
*macro
)
388 struct macro_group
*group
;
389 const char *format_ptr
;
395 group
= macro
->group
;
396 if (group
->uses_macro_p (x
, macro
->index
+ group
->num_builtins
))
399 format_ptr
= GET_RTX_FORMAT (BELLWETHER_CODE (GET_CODE (x
)));
400 for (i
= 0; format_ptr
[i
] != 0; i
++)
401 switch (format_ptr
[i
])
404 if (uses_macro_p (XEXP (x
, i
), macro
))
411 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
412 if (uses_macro_p (XVECEXP (x
, i
, j
), macro
))
422 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
423 has the form "&& ..." (as used in define_insn_and_splits), assume that
424 EXTRA is already satisfied. Empty strings are treated like "true". */
427 add_condition_to_string (const char *original
, const char *extra
)
429 if (original
!= 0 && original
[0] == '&' && original
[1] == '&')
431 return join_c_conditions (original
, extra
);
434 /* Like add_condition, but applied to all conditions in rtx X. */
437 add_condition_to_rtx (rtx x
, const char *extra
)
439 switch (GET_CODE (x
))
443 XSTR (x
, 2) = add_condition_to_string (XSTR (x
, 2), extra
);
447 case DEFINE_PEEPHOLE
:
448 case DEFINE_PEEPHOLE2
:
449 case DEFINE_COND_EXEC
:
450 XSTR (x
, 1) = add_condition_to_string (XSTR (x
, 1), extra
);
453 case DEFINE_INSN_AND_SPLIT
:
454 XSTR (x
, 2) = add_condition_to_string (XSTR (x
, 2), extra
);
455 XSTR (x
, 4) = add_condition_to_string (XSTR (x
, 4), extra
);
463 /* A htab_traverse callback. Search the EXPR_LIST given by DATA
464 for rtxes that use the macro in *SLOT. Replace each such rtx
465 with a list of expansions. */
468 apply_macro_traverse (void **slot
, void *data
)
470 struct mapping
*macro
;
472 rtx elem
, new_elem
, original
, x
;
474 macro
= (struct mapping
*) *slot
;
475 for (elem
= (rtx
) data
; elem
!= 0; elem
= XEXP (elem
, 1))
476 if (uses_macro_p (XEXP (elem
, 0), macro
))
478 original
= XEXP (elem
, 0);
479 for (v
= macro
->values
; v
!= 0; v
= v
->next
)
481 x
= apply_macro_to_rtx (original
, macro
, v
->number
);
482 add_condition_to_rtx (x
, v
->string
);
483 if (v
!= macro
->values
)
485 /* Insert a new EXPR_LIST node after ELEM and put the
486 new expansion there. */
487 new_elem
= rtx_alloc (EXPR_LIST
);
488 XEXP (new_elem
, 1) = XEXP (elem
, 1);
489 XEXP (elem
, 1) = new_elem
;
498 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
499 of the mapping, GROUP is the group to which it belongs, and INFILE
500 is the file that defined the mapping. */
502 static struct mapping
*
503 add_mapping (struct macro_group
*group
, htab_t table
,
504 const char *name
, FILE *infile
)
509 m
= XNEW (struct mapping
);
510 m
->name
= xstrdup (name
);
512 m
->index
= htab_elements (table
);
515 slot
= htab_find_slot (table
, m
, INSERT
);
517 fatal_with_file_and_line (infile
, "`%s' already defined", name
);
523 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
524 END_PTR points to the current null terminator for the list; return
525 a pointer the new null terminator. */
527 static struct map_value
**
528 add_map_value (struct map_value
**end_ptr
, int number
, const char *string
)
530 struct map_value
*value
;
532 value
= XNEW (struct map_value
);
534 value
->number
= number
;
535 value
->string
= string
;
541 /* Do one-time initialization of the mode and code attributes. */
544 initialize_macros (void)
546 struct mapping
*lower
, *upper
;
547 struct map_value
**lower_ptr
, **upper_ptr
;
551 modes
.attrs
= htab_create (13, def_hash
, def_name_eq_p
, 0);
552 modes
.macros
= htab_create (13, def_hash
, def_name_eq_p
, 0);
553 modes
.num_builtins
= MAX_MACHINE_MODE
;
554 modes
.find_builtin
= find_mode
;
555 modes
.uses_macro_p
= uses_mode_macro_p
;
556 modes
.apply_macro
= apply_mode_macro
;
558 codes
.attrs
= htab_create (13, def_hash
, def_name_eq_p
, 0);
559 codes
.macros
= htab_create (13, def_hash
, def_name_eq_p
, 0);
560 codes
.num_builtins
= NUM_RTX_CODE
;
561 codes
.find_builtin
= find_code
;
562 codes
.uses_macro_p
= uses_code_macro_p
;
563 codes
.apply_macro
= apply_code_macro
;
565 lower
= add_mapping (&modes
, modes
.attrs
, "mode", 0);
566 upper
= add_mapping (&modes
, modes
.attrs
, "MODE", 0);
567 lower_ptr
= &lower
->values
;
568 upper_ptr
= &upper
->values
;
569 for (i
= 0; i
< MAX_MACHINE_MODE
; i
++)
571 copy
= xstrdup (GET_MODE_NAME (i
));
572 for (p
= copy
; *p
!= 0; p
++)
575 upper_ptr
= add_map_value (upper_ptr
, i
, GET_MODE_NAME (i
));
576 lower_ptr
= add_map_value (lower_ptr
, i
, copy
);
579 lower
= add_mapping (&codes
, codes
.attrs
, "code", 0);
580 upper
= add_mapping (&codes
, codes
.attrs
, "CODE", 0);
581 lower_ptr
= &lower
->values
;
582 upper_ptr
= &upper
->values
;
583 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
585 copy
= xstrdup (GET_RTX_NAME (i
));
586 for (p
= copy
; *p
!= 0; p
++)
589 lower_ptr
= add_map_value (lower_ptr
, i
, GET_RTX_NAME (i
));
590 upper_ptr
= add_map_value (upper_ptr
, i
, copy
);
594 /* Return a hash value for the pointer pointed to by DEF. */
597 leading_ptr_hash (const void *def
)
599 return htab_hash_pointer (*(const void *const *) def
);
602 /* Return true if DEF1 and DEF2 are pointers to the same pointer. */
605 leading_ptr_eq_p (const void *def1
, const void *def2
)
607 return *(const void *const *) def1
== *(const void *const *) def2
;
610 /* Associate PTR with the file position given by FILENAME and LINENO. */
613 set_rtx_ptr_loc (const void *ptr
, const char *filename
, int lineno
)
617 loc
= (struct ptr_loc
*) obstack_alloc (&ptr_loc_obstack
,
618 sizeof (struct ptr_loc
));
620 loc
->filename
= filename
;
621 loc
->lineno
= lineno
;
622 *htab_find_slot (ptr_locs
, loc
, INSERT
) = loc
;
625 /* Return the position associated with pointer PTR. Return null if no
628 static const struct ptr_loc
*
629 get_rtx_ptr_loc (const void *ptr
)
631 return (const struct ptr_loc
*) htab_find (ptr_locs
, &ptr
);
634 /* Associate NEW_PTR with the same file position as OLD_PTR. */
637 copy_rtx_ptr_loc (const void *new_ptr
, const void *old_ptr
)
639 const struct ptr_loc
*loc
= get_rtx_ptr_loc (old_ptr
);
641 set_rtx_ptr_loc (new_ptr
, loc
->filename
, loc
->lineno
);
644 /* If PTR is associated with a known file position, print a #line
648 print_rtx_ptr_loc (const void *ptr
)
650 const struct ptr_loc
*loc
= get_rtx_ptr_loc (ptr
);
652 printf ("#line %d \"%s\"\n", loc
->lineno
, loc
->filename
);
655 /* Return a condition that satisfies both COND1 and COND2. Either string
656 may be null or empty. */
659 join_c_conditions (const char *cond1
, const char *cond2
)
664 if (cond1
== 0 || cond1
[0] == 0)
667 if (cond2
== 0 || cond2
[0] == 0)
670 result
= concat ("(", cond1
, ") && (", cond2
, ")", NULL
);
671 obstack_ptr_grow (&joined_conditions_obstack
, result
);
672 obstack_ptr_grow (&joined_conditions_obstack
, cond1
);
673 obstack_ptr_grow (&joined_conditions_obstack
, cond2
);
674 entry
= obstack_finish (&joined_conditions_obstack
);
675 *htab_find_slot (joined_conditions
, entry
, INSERT
) = entry
;
679 /* Print condition COND, wrapped in brackets. If COND was created by
680 join_c_conditions, recursively invoke this function for the original
681 conditions and join the result with "&&". Otherwise print a #line
682 directive for COND if its original file position is known. */
685 print_c_condition (const char *cond
)
687 const void **halves
= htab_find (joined_conditions
, &cond
);
691 print_c_condition (halves
[1]);
693 print_c_condition (halves
[2]);
699 print_rtx_ptr_loc (cond
);
700 printf ("(%s)", cond
);
704 /* Read chars from INFILE until a non-whitespace char
705 and return that. Comments, both Lisp style and C style,
706 are treated as whitespace.
707 Tools such as genflags use this function. */
710 read_skip_spaces (FILE *infile
)
723 case ' ': case '\t': case '\f': case '\r':
729 while (c
!= '\n' && c
!= EOF
);
738 fatal_expected_char (infile
, '*', c
);
741 while ((c
= getc (infile
)) && c
!= EOF
)
745 else if (prevc
== '*' && c
== '/')
758 /* Read an rtx code name into the buffer STR[].
759 It is terminated by any of the punctuation chars of rtx printed syntax. */
762 read_name (char *str
, FILE *infile
)
767 c
= read_skip_spaces (infile
);
772 if (c
== ' ' || c
== '\n' || c
== '\t' || c
== '\f' || c
== '\r')
774 if (c
== ':' || c
== ')' || c
== ']' || c
== '"' || c
== '/'
775 || c
== '(' || c
== '[')
784 fatal_with_file_and_line (infile
, "missing name or number");
792 /* Do constant expansion. */
793 struct md_constant
*def
;
798 struct md_constant tmp_def
;
801 def
= (struct md_constant
*) htab_find (md_constants
, &tmp_def
);
810 /* Subroutine of the string readers. Handles backslash escapes.
811 Caller has read the backslash, but not placed it into the obstack. */
813 read_escape (FILE *infile
)
815 int c
= getc (infile
);
819 /* Backslash-newline is replaced by nothing, as in C. */
824 /* \" \' \\ are replaced by the second character. */
830 /* Standard C string escapes:
833 all are passed through to the output string unmolested.
834 In normal use these wind up in a string constant processed
835 by the C compiler, which will translate them appropriately.
836 We do not bother checking that \[0-7] are followed by up to
837 two octal digits, or that \x is followed by N hex digits.
838 \? \u \U are left out because they are not in traditional C. */
839 case 'a': case 'b': case 'f': case 'n': case 'r': case 't': case 'v':
840 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
842 obstack_1grow (&string_obstack
, '\\');
845 /* \; makes stuff for a C string constant containing
848 obstack_grow (&string_obstack
, "\\n\\t", 4);
851 /* pass anything else through, but issue a warning. */
853 fprintf (stderr
, "%s:%d: warning: unrecognized escape \\%c\n",
854 read_rtx_filename
, read_rtx_lineno
, c
);
855 obstack_1grow (&string_obstack
, '\\');
859 obstack_1grow (&string_obstack
, c
);
863 /* Read a double-quoted string onto the obstack. Caller has scanned
864 the leading quote. */
866 read_quoted_string (FILE *infile
)
872 c
= getc (infile
); /* Read the string */
877 read_escape (infile
);
883 obstack_1grow (&string_obstack
, c
);
886 obstack_1grow (&string_obstack
, 0);
887 return (char *) obstack_finish (&string_obstack
);
890 /* Read a braced string (a la Tcl) onto the string obstack. Caller
891 has scanned the leading brace. Note that unlike quoted strings,
892 the outermost braces _are_ included in the string constant. */
894 read_braced_string (FILE *infile
)
897 int brace_depth
= 1; /* caller-processed */
898 unsigned long starting_read_rtx_lineno
= read_rtx_lineno
;
900 obstack_1grow (&string_obstack
, '{');
903 c
= getc (infile
); /* Read the string */
913 read_escape (infile
);
917 fatal_with_file_and_line
918 (infile
, "missing closing } for opening brace on line %lu",
919 starting_read_rtx_lineno
);
921 obstack_1grow (&string_obstack
, c
);
924 obstack_1grow (&string_obstack
, 0);
925 return (char *) obstack_finish (&string_obstack
);
928 /* Read some kind of string constant. This is the high-level routine
929 used by read_rtx. It handles surrounding parentheses, leading star,
930 and dispatch to the appropriate string constant reader. */
933 read_string (FILE *infile
, int star_if_braced
)
939 c
= read_skip_spaces (infile
);
943 c
= read_skip_spaces (infile
);
946 old_lineno
= read_rtx_lineno
;
948 stringbuf
= read_quoted_string (infile
);
952 obstack_1grow (&string_obstack
, '*');
953 stringbuf
= read_braced_string (infile
);
956 fatal_with_file_and_line (infile
, "expected `\"' or `{', found `%c'", c
);
960 c
= read_skip_spaces (infile
);
962 fatal_expected_char (infile
, ')', c
);
965 set_rtx_ptr_loc (stringbuf
, read_rtx_filename
, old_lineno
);
969 /* Provide a version of a function to read a long long if the system does
971 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !defined(HAVE_ATOLL) && !defined(HAVE_ATOQ)
972 HOST_WIDE_INT
atoll (const char *);
975 atoll (const char *p
)
978 HOST_WIDE_INT tmp_wide
;
990 HOST_WIDE_INT new_wide
= tmp_wide
*10 + (*p
- '0');
991 if (new_wide
< tmp_wide
)
993 /* Return INT_MAX equiv on overflow. */
994 tmp_wide
= (~(unsigned HOST_WIDE_INT
) 0) >> 1;
1002 tmp_wide
= -tmp_wide
;
1007 /* Given an object that starts with a char * name field, return a hash
1008 code for its name. */
1010 def_hash (const void *def
)
1013 const char *string
= *(const char *const *) def
;
1015 for (result
= i
= 0; *string
++ != '\0'; i
++)
1016 result
+= ((unsigned char) *string
<< (i
% CHAR_BIT
));
1020 /* Given two objects that start with char * name fields, return true if
1021 they have the same name. */
1023 def_name_eq_p (const void *def1
, const void *def2
)
1025 return ! strcmp (*(const char *const *) def1
,
1026 *(const char *const *) def2
);
1029 /* INFILE is a FILE pointer to read text from. TMP_CHAR is a buffer suitable
1030 to read a name or number into. Process a define_constants directive,
1031 starting with the optional space after the "define_constants". */
1033 read_constants (FILE *infile
, char *tmp_char
)
1038 c
= read_skip_spaces (infile
);
1040 fatal_expected_char (infile
, '[', c
);
1041 defs
= md_constants
;
1043 defs
= htab_create (32, def_hash
, def_name_eq_p
, (htab_del
) 0);
1044 /* Disable constant expansion during definition processing. */
1046 while ( (c
= read_skip_spaces (infile
)) != ']')
1048 struct md_constant
*def
;
1052 fatal_expected_char (infile
, '(', c
);
1053 def
= XNEW (struct md_constant
);
1054 def
->name
= tmp_char
;
1055 read_name (tmp_char
, infile
);
1056 entry_ptr
= htab_find_slot (defs
, def
, INSERT
);
1058 def
->name
= xstrdup (tmp_char
);
1059 c
= read_skip_spaces (infile
);
1061 read_name (tmp_char
, infile
);
1064 def
->value
= xstrdup (tmp_char
);
1069 def
= (struct md_constant
*) *entry_ptr
;
1070 if (strcmp (def
->value
, tmp_char
))
1071 fatal_with_file_and_line (infile
,
1072 "redefinition of %s, was %s, now %s",
1073 def
->name
, def
->value
, tmp_char
);
1075 c
= read_skip_spaces (infile
);
1077 fatal_expected_char (infile
, ')', c
);
1079 md_constants
= defs
;
1080 c
= read_skip_spaces (infile
);
1082 fatal_expected_char (infile
, ')', c
);
1085 /* For every constant definition, call CALLBACK with two arguments:
1086 a pointer a pointer to the constant definition and INFO.
1087 Stops when CALLBACK returns zero. */
1089 traverse_md_constants (htab_trav callback
, void *info
)
1092 htab_traverse (md_constants
, callback
, info
);
1096 validate_const_int (FILE *infile
, const char *string
)
1102 while (*cp
&& ISSPACE (*cp
))
1104 if (*cp
== '-' || *cp
== '+')
1109 if (! ISDIGIT (*cp
))
1112 fatal_with_file_and_line (infile
, "invalid decimal constant \"%s\"\n", string
);
1115 /* Search GROUP for a mode or code called NAME and return its numerical
1116 identifier. INFILE is the file that contained NAME. */
1119 find_macro (struct macro_group
*group
, const char *name
, FILE *infile
)
1123 m
= (struct mapping
*) htab_find (group
->macros
, &name
);
1125 return m
->index
+ group
->num_builtins
;
1126 return group
->find_builtin (name
, infile
);
1129 /* Finish reading a declaration of the form:
1131 (define... <name> [<value1> ... <valuen>])
1133 from INFILE, where each <valuei> is either a bare symbol name or a
1134 "(<name> <string>)" pair. The "(define..." part has already been read.
1136 Represent the declaration as a "mapping" structure; add it to TABLE
1137 (which belongs to GROUP) and return it. */
1139 static struct mapping
*
1140 read_mapping (struct macro_group
*group
, htab_t table
, FILE *infile
)
1144 struct map_value
**end_ptr
;
1148 /* Read the mapping name and create a structure for it. */
1149 read_name (tmp_char
, infile
);
1150 m
= add_mapping (group
, table
, tmp_char
, infile
);
1152 c
= read_skip_spaces (infile
);
1154 fatal_expected_char (infile
, '[', c
);
1156 /* Read each value. */
1157 end_ptr
= &m
->values
;
1158 c
= read_skip_spaces (infile
);
1163 /* A bare symbol name that is implicitly paired to an
1166 read_name (tmp_char
, infile
);
1171 /* A "(name string)" pair. */
1172 read_name (tmp_char
, infile
);
1173 string
= read_string (infile
, false);
1174 c
= read_skip_spaces (infile
);
1176 fatal_expected_char (infile
, ')', c
);
1178 number
= group
->find_builtin (tmp_char
, infile
);
1179 end_ptr
= add_map_value (end_ptr
, number
, string
);
1180 c
= read_skip_spaces (infile
);
1184 c
= read_skip_spaces (infile
);
1186 fatal_expected_char (infile
, ')', c
);
1191 /* Check newly-created code macro MACRO to see whether every code has the
1192 same format. Initialize the macro's entry in bellwether_codes. */
1195 check_code_macro (struct mapping
*macro
, FILE *infile
)
1197 struct map_value
*v
;
1198 enum rtx_code bellwether
;
1200 bellwether
= macro
->values
->number
;
1201 for (v
= macro
->values
->next
; v
!= 0; v
= v
->next
)
1202 if (strcmp (GET_RTX_FORMAT (bellwether
), GET_RTX_FORMAT (v
->number
)) != 0)
1203 fatal_with_file_and_line (infile
, "code macro `%s' combines "
1204 "different rtx formats", macro
->name
);
1206 bellwether_codes
= XRESIZEVEC (enum rtx_code
, bellwether_codes
,
1208 bellwether_codes
[macro
->index
] = bellwether
;
1211 /* Read an rtx in printed representation from INFILE and store its
1212 core representation in *X. Also store the line number of the
1213 opening '(' in *LINENO. Return true on success or false if the
1214 end of file has been reached.
1216 read_rtx is not used in the compiler proper, but rather in
1217 the utilities gen*.c that construct C code from machine descriptions. */
1220 read_rtx (FILE *infile
, rtx
*x
, int *lineno
)
1222 static rtx queue_head
, queue_next
;
1223 static int queue_lineno
;
1226 /* Do one-time initialization. */
1227 if (queue_head
== 0)
1229 initialize_macros ();
1230 obstack_init (&string_obstack
);
1231 queue_head
= rtx_alloc (EXPR_LIST
);
1232 ptr_locs
= htab_create (161, leading_ptr_hash
, leading_ptr_eq_p
, 0);
1233 obstack_init (&ptr_loc_obstack
);
1234 joined_conditions
= htab_create (161, leading_ptr_hash
,
1235 leading_ptr_eq_p
, 0);
1236 obstack_init (&joined_conditions_obstack
);
1239 if (queue_next
== 0)
1241 c
= read_skip_spaces (infile
);
1246 queue_next
= queue_head
;
1247 queue_lineno
= read_rtx_lineno
;
1248 XEXP (queue_next
, 0) = read_rtx_1 (infile
);
1249 XEXP (queue_next
, 1) = 0;
1251 htab_traverse (modes
.macros
, apply_macro_traverse
, queue_next
);
1252 htab_traverse (codes
.macros
, apply_macro_traverse
, queue_next
);
1255 *x
= XEXP (queue_next
, 0);
1256 *lineno
= queue_lineno
;
1257 queue_next
= XEXP (queue_next
, 1);
1262 /* Subroutine of read_rtx that reads one construct from INFILE but
1263 doesn't apply any macros. */
1266 read_rtx_1 (FILE *infile
)
1269 RTX_CODE real_code
, bellwether_code
;
1270 const char *format_ptr
;
1271 /* tmp_char is a buffer used for reading decimal integers
1272 and names of rtx types and machine modes.
1273 Therefore, 256 must be enough. */
1278 HOST_WIDE_INT tmp_wide
;
1280 /* Linked list structure for making RTXs: */
1283 struct rtx_list
*next
;
1284 rtx value
; /* Value of this node. */
1288 c
= read_skip_spaces (infile
); /* Should be open paren. */
1290 fatal_expected_char (infile
, '(', c
);
1292 read_name (tmp_char
, infile
);
1293 if (strcmp (tmp_char
, "nil") == 0)
1295 /* (nil) stands for an expression that isn't there. */
1296 c
= read_skip_spaces (infile
);
1298 fatal_expected_char (infile
, ')', c
);
1301 if (strcmp (tmp_char
, "define_constants") == 0)
1303 read_constants (infile
, tmp_char
);
1306 if (strcmp (tmp_char
, "define_mode_attr") == 0)
1308 read_mapping (&modes
, modes
.attrs
, infile
);
1311 if (strcmp (tmp_char
, "define_mode_macro") == 0)
1313 read_mapping (&modes
, modes
.macros
, infile
);
1316 if (strcmp (tmp_char
, "define_code_attr") == 0)
1318 read_mapping (&codes
, codes
.attrs
, infile
);
1321 if (strcmp (tmp_char
, "define_code_macro") == 0)
1323 check_code_macro (read_mapping (&codes
, codes
.macros
, infile
), infile
);
1326 real_code
= find_macro (&codes
, tmp_char
, infile
);
1327 bellwether_code
= BELLWETHER_CODE (real_code
);
1329 /* If we end up with an insn expression then we free this space below. */
1330 return_rtx
= rtx_alloc (bellwether_code
);
1331 format_ptr
= GET_RTX_FORMAT (bellwether_code
);
1332 PUT_CODE (return_rtx
, real_code
);
1334 /* If what follows is `: mode ', read it and
1335 store the mode in the rtx. */
1337 i
= read_skip_spaces (infile
);
1340 read_name (tmp_char
, infile
);
1341 PUT_MODE (return_rtx
, find_macro (&modes
, tmp_char
, infile
));
1346 for (i
= 0; format_ptr
[i
] != 0; i
++)
1347 switch (format_ptr
[i
])
1349 /* 0 means a field for internal use only.
1350 Don't expect it to be present in the input. */
1356 XEXP (return_rtx
, i
) = read_rtx_1 (infile
);
1360 /* 'V' is an optional vector: if a closeparen follows,
1361 just store NULL for this element. */
1362 c
= read_skip_spaces (infile
);
1366 XVEC (return_rtx
, i
) = 0;
1369 /* Now process the vector. */
1373 /* Obstack to store scratch vector in. */
1374 struct obstack vector_stack
;
1375 int list_counter
= 0;
1376 rtvec return_vec
= NULL_RTVEC
;
1378 c
= read_skip_spaces (infile
);
1380 fatal_expected_char (infile
, '[', c
);
1382 /* Add expressions to a list, while keeping a count. */
1383 obstack_init (&vector_stack
);
1384 while ((c
= read_skip_spaces (infile
)) && c
!= ']')
1388 obstack_ptr_grow (&vector_stack
, read_rtx_1 (infile
));
1390 if (list_counter
> 0)
1392 return_vec
= rtvec_alloc (list_counter
);
1393 memcpy (&return_vec
->elem
[0], obstack_finish (&vector_stack
),
1394 list_counter
* sizeof (rtx
));
1396 else if (format_ptr
[i
] == 'E')
1397 fatal_with_file_and_line (infile
,
1398 "vector must have at least one element");
1399 XVEC (return_rtx
, i
) = return_vec
;
1400 obstack_free (&vector_stack
, NULL
);
1401 /* close bracket gotten */
1412 c
= read_skip_spaces (infile
);
1416 /* 'S' fields are optional and should be NULL if no string
1417 was given. Also allow normal 's' and 'T' strings to be
1418 omitted, treating them in the same way as empty strings. */
1419 XSTR (return_rtx
, i
) = (format_ptr
[i
] == 'S' ? NULL
: "");
1423 /* The output template slot of a DEFINE_INSN,
1424 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
1425 gets a star inserted as its first character, if it is
1426 written with a brace block instead of a string constant. */
1427 star_if_braced
= (format_ptr
[i
] == 'T');
1429 stringbuf
= read_string (infile
, star_if_braced
);
1431 /* For insn patterns, we want to provide a default name
1432 based on the file and line, like "*foo.md:12", if the
1433 given name is blank. These are only for define_insn and
1434 define_insn_and_split, to aid debugging. */
1435 if (*stringbuf
== '\0'
1437 && (GET_CODE (return_rtx
) == DEFINE_INSN
1438 || GET_CODE (return_rtx
) == DEFINE_INSN_AND_SPLIT
))
1441 const char *fn
= (read_rtx_filename
? read_rtx_filename
: "rtx");
1443 for (slash
= fn
; *slash
; slash
++)
1444 if (*slash
== '/' || *slash
== '\\' || *slash
== ':')
1446 obstack_1grow (&string_obstack
, '*');
1447 obstack_grow (&string_obstack
, fn
, strlen (fn
));
1448 sprintf (line_name
, ":%d", read_rtx_lineno
);
1449 obstack_grow (&string_obstack
, line_name
, strlen (line_name
)+1);
1450 stringbuf
= (char *) obstack_finish (&string_obstack
);
1454 XTMPL (return_rtx
, i
) = stringbuf
;
1456 XSTR (return_rtx
, i
) = stringbuf
;
1461 read_name (tmp_char
, infile
);
1462 validate_const_int (infile
, tmp_char
);
1463 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1464 tmp_wide
= atoi (tmp_char
);
1466 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1467 tmp_wide
= atol (tmp_char
);
1469 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1470 But prefer not to use our hand-rolled function above either. */
1471 #if defined(HAVE_ATOLL) || !defined(HAVE_ATOQ)
1472 tmp_wide
= atoll (tmp_char
);
1474 tmp_wide
= atoq (tmp_char
);
1478 XWINT (return_rtx
, i
) = tmp_wide
;
1483 read_name (tmp_char
, infile
);
1484 validate_const_int (infile
, tmp_char
);
1485 tmp_int
= atoi (tmp_char
);
1486 XINT (return_rtx
, i
) = tmp_int
;
1491 "switch format wrong in rtl.read_rtx(). format was: %c.\n",
1493 fprintf (stderr
, "\tfile position: %ld\n", ftell (infile
));
1497 c
= read_skip_spaces (infile
);
1499 fatal_expected_char (infile
, ')', c
);