2 Copyright (C) 1987, 1988, 1991, 1994, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2007, 2008
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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 /* Disable rtl checking; it conflicts with the iterator handling. */
25 #undef ENABLE_RTL_CHECKING
28 #include "coretypes.h"
34 #include "gensupport.h"
36 static htab_t md_constants;
38 /* One element in a singly-linked list of (integer, string) pairs. */
40 struct map_value *next;
45 /* Maps an iterator or attribute name to a list of (integer, string) pairs.
46 The integers are mode or code values; the strings are either C conditions
47 or attribute values. */
49 /* The name of the iterator or attribute. */
52 /* The group (modes or codes) to which the iterator or attribute belongs. */
53 struct iterator_group *group;
55 /* Gives a unique number to the attribute or iterator. Numbers are
56 allocated consecutively, starting at 0. */
59 /* The list of (integer, string) pairs. */
60 struct map_value *values;
63 /* A structure for abstracting the common parts of code and mode iterators. */
64 struct iterator_group {
65 /* Tables of "mapping" structures, one for attributes and one for iterators. */
66 htab_t attrs, iterators;
68 /* The number of "real" modes or codes (and by extension, the first
69 number available for use as an iterator placeholder). */
72 /* Treat the given string as the name of a standard mode or code and
73 return its integer value. Use the given file for error reporting. */
74 int (*find_builtin) (const char *, FILE *);
76 /* Return true if the given rtx uses the given mode or code. */
77 bool (*uses_iterator_p) (rtx, int);
79 /* Make the given rtx use the given mode or code. */
80 void (*apply_iterator) (rtx, int);
83 /* A structure used to pass data from read_rtx to apply_iterator_traverse
85 struct iterator_traverse_data {
86 /* Instruction queue. */
88 /* Attributes seen for modes. */
89 struct map_value *mode_maps;
92 /* The last unknown attribute used as a mode. */
93 const char *unknown_mode_attr;
96 /* If CODE is the number of a code iterator, return a real rtx code that
97 has the same format. Return CODE otherwise. */
98 #define BELLWETHER_CODE(CODE) \
99 ((CODE) < NUM_RTX_CODE ? CODE : bellwether_codes[CODE - NUM_RTX_CODE])
101 static int find_mode (const char *, FILE *);
102 static bool uses_mode_iterator_p (rtx, int);
103 static void apply_mode_iterator (rtx, int);
104 static int find_code (const char *, FILE *);
105 static bool uses_code_iterator_p (rtx, int);
106 static void apply_code_iterator (rtx, int);
107 static const char *apply_iterator_to_string (const char *, struct mapping *, int);
108 static rtx apply_iterator_to_rtx (rtx, struct mapping *, int,
109 struct map_value *, FILE *, const char **);
110 static bool uses_iterator_p (rtx, struct mapping *);
111 static const char *add_condition_to_string (const char *, const char *);
112 static void add_condition_to_rtx (rtx, const char *);
113 static int apply_iterator_traverse (void **, void *);
114 static struct mapping *add_mapping (struct iterator_group *, htab_t t,
115 const char *, FILE *);
116 static struct map_value **add_map_value (struct map_value **,
118 static void initialize_iterators (void);
119 static void read_name (char *, FILE *);
120 static hashval_t def_hash (const void *);
121 static int def_name_eq_p (const void *, const void *);
122 static void read_constants (FILE *infile, char *tmp_char);
123 static void read_conditions (FILE *infile, char *tmp_char);
124 static void validate_const_int (FILE *, const char *);
125 static int find_iterator (struct iterator_group *, const char *, FILE *);
126 static struct mapping *read_mapping (struct iterator_group *, htab_t, FILE *);
127 static void check_code_iterator (struct mapping *, FILE *);
128 static rtx read_rtx_1 (FILE *, struct map_value **);
129 static rtx read_rtx_variadic (FILE *, struct map_value **, rtx);
131 /* The mode and code iterator structures. */
132 static struct iterator_group modes, codes;
134 /* Index I is the value of BELLWETHER_CODE (I + NUM_RTX_CODE). */
135 static enum rtx_code *bellwether_codes;
137 /* Implementations of the iterator_group callbacks for modes. */
140 find_mode (const char *name, FILE *infile)
144 for (i = 0; i < NUM_MACHINE_MODES; i++)
145 if (strcmp (GET_MODE_NAME (i), name) == 0)
148 fatal_with_file_and_line (infile, "unknown mode `%s'", name);
152 uses_mode_iterator_p (rtx x, int mode)
154 return (int) GET_MODE (x) == mode;
158 apply_mode_iterator (rtx x, int mode)
160 PUT_MODE (x, (enum machine_mode) mode);
163 /* Implementations of the iterator_group callbacks for codes. */
166 find_code (const char *name, FILE *infile)
170 for (i = 0; i < NUM_RTX_CODE; i++)
171 if (strcmp (GET_RTX_NAME (i), name) == 0)
174 fatal_with_file_and_line (infile, "unknown rtx code `%s'", name);
178 uses_code_iterator_p (rtx x, int code)
180 return (int) GET_CODE (x) == code;
184 apply_code_iterator (rtx x, int code)
186 PUT_CODE (x, (enum rtx_code) code);
189 /* Map a code or mode attribute string P to the underlying string for
190 ITERATOR and VALUE. */
192 static struct map_value *
193 map_attr_string (const char *p, struct mapping *iterator, int value)
199 /* If there's a "iterator:" prefix, check whether the iterator name matches.
200 Set ATTR to the start of the attribute name. */
201 attr = strchr (p, ':');
206 if (strncmp (p, iterator->name, attr - p) != 0
207 || iterator->name[attr - p] != 0)
212 /* Find the attribute specification. */
213 m = (struct mapping *) htab_find (iterator->group->attrs, &attr);
217 /* Find the attribute value for VALUE. */
218 for (v = m->values; v != 0; v = v->next)
219 if (v->number == value)
225 /* Given an attribute string used as a machine mode, return an index
226 to store in the machine mode to be translated by
227 apply_iterator_to_rtx. */
230 mode_attr_index (struct map_value **mode_maps, const char *string)
233 struct map_value *mv;
235 /* Copy the attribute string into permanent storage, without the
236 angle brackets around it. */
237 obstack_grow0 (&string_obstack, string + 1, strlen (string) - 2);
238 p = XOBFINISH (&string_obstack, char *);
240 mv = XNEW (struct map_value);
241 mv->number = *mode_maps == 0 ? 0 : (*mode_maps)->number + 1;
243 mv->next = *mode_maps;
246 /* We return a code which we can map back into this string: the
247 number of machine modes + the number of mode iterators + the index
249 return MAX_MACHINE_MODE + htab_elements (modes.iterators) + mv->number;
252 /* Apply MODE_MAPS to the top level of X, expanding cases where an
253 attribute is used for a mode. ITERATOR is the current iterator we are
254 expanding, and VALUE is the value to which we are expanding it.
255 INFILE is used for error messages. This sets *UNKNOWN to true if
256 we find a mode attribute which has not yet been defined, and does
257 not change it otherwise. */
260 apply_mode_maps (rtx x, struct map_value *mode_maps, struct mapping *iterator,
261 int value, FILE *infile, const char **unknown)
265 struct map_value *pm;
267 offset = MAX_MACHINE_MODE + htab_elements (modes.iterators);
268 if (GET_MODE (x) < offset)
271 indx = GET_MODE (x) - offset;
272 for (pm = mode_maps; pm; pm = pm->next)
274 if (pm->number == indx)
278 v = map_attr_string (pm->string, iterator, value);
280 PUT_MODE (x, (enum machine_mode) find_mode (v->string, infile));
282 *unknown = pm->string;
288 /* Given that ITERATOR is being expanded as VALUE, apply the appropriate
289 string substitutions to STRING. Return the new string if any changes
290 were needed, otherwise return STRING itself. */
293 apply_iterator_to_string (const char *string, struct mapping *iterator, int value)
295 char *base, *copy, *p, *start, *end;
301 base = p = copy = ASTRDUP (string);
302 while ((start = strchr (p, '<')) && (end = strchr (start, '>')))
307 v = map_attr_string (p, iterator, value);
312 /* Add everything between the last copied byte and the '<',
313 then add in the attribute value. */
314 obstack_grow (&string_obstack, base, start - base);
315 obstack_grow (&string_obstack, v->string, strlen (v->string));
320 obstack_grow (&string_obstack, base, strlen (base) + 1);
321 copy = XOBFINISH (&string_obstack, char *);
322 copy_md_ptr_loc (copy, string);
328 /* Return a copy of ORIGINAL in which all uses of ITERATOR have been
329 replaced by VALUE. MODE_MAPS holds information about attribute
330 strings used for modes. INFILE is used for error messages. This
331 sets *UNKNOWN_MODE_ATTR to the value of an unknown mode attribute,
332 and does not change it otherwise. */
335 apply_iterator_to_rtx (rtx original, struct mapping *iterator, int value,
336 struct map_value *mode_maps, FILE *infile,
337 const char **unknown_mode_attr)
339 struct iterator_group *group;
340 const char *format_ptr;
343 enum rtx_code bellwether_code;
348 /* Create a shallow copy of ORIGINAL. */
349 bellwether_code = BELLWETHER_CODE (GET_CODE (original));
350 x = rtx_alloc (bellwether_code);
351 memcpy (x, original, RTX_CODE_SIZE (bellwether_code));
353 /* Change the mode or code itself. */
354 group = iterator->group;
355 if (group->uses_iterator_p (x, iterator->index + group->num_builtins))
356 group->apply_iterator (x, value);
359 apply_mode_maps (x, mode_maps, iterator, value, infile, unknown_mode_attr);
361 /* Change each string and recursively change each rtx. */
362 format_ptr = GET_RTX_FORMAT (bellwether_code);
363 for (i = 0; format_ptr[i] != 0; i++)
364 switch (format_ptr[i])
367 XTMPL (x, i) = apply_iterator_to_string (XTMPL (x, i), iterator, value);
372 XSTR (x, i) = apply_iterator_to_string (XSTR (x, i), iterator, value);
376 XEXP (x, i) = apply_iterator_to_rtx (XEXP (x, i), iterator, value,
383 if (XVEC (original, i))
385 XVEC (x, i) = rtvec_alloc (XVECLEN (original, i));
386 for (j = 0; j < XVECLEN (x, i); j++)
387 XVECEXP (x, i, j) = apply_iterator_to_rtx (XVECEXP (original, i, j),
388 iterator, value, mode_maps,
400 /* Return true if X (or some subexpression of X) uses iterator ITERATOR. */
403 uses_iterator_p (rtx x, struct mapping *iterator)
405 struct iterator_group *group;
406 const char *format_ptr;
412 group = iterator->group;
413 if (group->uses_iterator_p (x, iterator->index + group->num_builtins))
416 format_ptr = GET_RTX_FORMAT (BELLWETHER_CODE (GET_CODE (x)));
417 for (i = 0; format_ptr[i] != 0; i++)
418 switch (format_ptr[i])
421 if (uses_iterator_p (XEXP (x, i), iterator))
428 for (j = 0; j < XVECLEN (x, i); j++)
429 if (uses_iterator_p (XVECEXP (x, i, j), iterator))
439 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
440 has the form "&& ..." (as used in define_insn_and_splits), assume that
441 EXTRA is already satisfied. Empty strings are treated like "true". */
444 add_condition_to_string (const char *original, const char *extra)
446 if (original != 0 && original[0] == '&' && original[1] == '&')
448 return join_c_conditions (original, extra);
451 /* Like add_condition, but applied to all conditions in rtx X. */
454 add_condition_to_rtx (rtx x, const char *extra)
456 switch (GET_CODE (x))
460 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
464 case DEFINE_PEEPHOLE:
465 case DEFINE_PEEPHOLE2:
466 case DEFINE_COND_EXEC:
467 XSTR (x, 1) = add_condition_to_string (XSTR (x, 1), extra);
470 case DEFINE_INSN_AND_SPLIT:
471 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
472 XSTR (x, 4) = add_condition_to_string (XSTR (x, 4), extra);
480 /* A htab_traverse callback. Search the EXPR_LIST given by DATA
481 for rtxes that use the iterator in *SLOT. Replace each such rtx
482 with a list of expansions. */
485 apply_iterator_traverse (void **slot, void *data)
487 struct iterator_traverse_data *mtd = (struct iterator_traverse_data *) data;
488 struct mapping *iterator;
490 rtx elem, new_elem, original, x;
492 iterator = (struct mapping *) *slot;
493 for (elem = mtd->queue; elem != 0; elem = XEXP (elem, 1))
494 if (uses_iterator_p (XEXP (elem, 0), iterator))
496 /* For each iterator we expand, we set UNKNOWN_MODE_ATTR to NULL.
497 If apply_iterator_rtx finds an unknown attribute for a mode,
498 it will set it to the attribute. We want to know whether
499 the attribute is unknown after we have expanded all
500 possible iterators, so setting it to NULL here gives us the
501 right result when the hash table traversal is complete. */
502 mtd->unknown_mode_attr = NULL;
504 original = XEXP (elem, 0);
505 for (v = iterator->values; v != 0; v = v->next)
507 x = apply_iterator_to_rtx (original, iterator, v->number,
508 mtd->mode_maps, mtd->infile,
509 &mtd->unknown_mode_attr);
510 add_condition_to_rtx (x, v->string);
511 if (v != iterator->values)
513 /* Insert a new EXPR_LIST node after ELEM and put the
514 new expansion there. */
515 new_elem = rtx_alloc (EXPR_LIST);
516 XEXP (new_elem, 1) = XEXP (elem, 1);
517 XEXP (elem, 1) = new_elem;
526 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
527 of the mapping, GROUP is the group to which it belongs, and INFILE
528 is the file that defined the mapping. */
530 static struct mapping *
531 add_mapping (struct iterator_group *group, htab_t table,
532 const char *name, FILE *infile)
537 m = XNEW (struct mapping);
538 m->name = xstrdup (name);
540 m->index = htab_elements (table);
543 slot = htab_find_slot (table, m, INSERT);
545 fatal_with_file_and_line (infile, "`%s' already defined", name);
551 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
552 END_PTR points to the current null terminator for the list; return
553 a pointer the new null terminator. */
555 static struct map_value **
556 add_map_value (struct map_value **end_ptr, int number, const char *string)
558 struct map_value *value;
560 value = XNEW (struct map_value);
562 value->number = number;
563 value->string = string;
569 /* Do one-time initialization of the mode and code attributes. */
572 initialize_iterators (void)
574 struct mapping *lower, *upper;
575 struct map_value **lower_ptr, **upper_ptr;
579 modes.attrs = htab_create (13, def_hash, def_name_eq_p, 0);
580 modes.iterators = htab_create (13, def_hash, def_name_eq_p, 0);
581 modes.num_builtins = MAX_MACHINE_MODE;
582 modes.find_builtin = find_mode;
583 modes.uses_iterator_p = uses_mode_iterator_p;
584 modes.apply_iterator = apply_mode_iterator;
586 codes.attrs = htab_create (13, def_hash, def_name_eq_p, 0);
587 codes.iterators = htab_create (13, def_hash, def_name_eq_p, 0);
588 codes.num_builtins = NUM_RTX_CODE;
589 codes.find_builtin = find_code;
590 codes.uses_iterator_p = uses_code_iterator_p;
591 codes.apply_iterator = apply_code_iterator;
593 lower = add_mapping (&modes, modes.attrs, "mode", 0);
594 upper = add_mapping (&modes, modes.attrs, "MODE", 0);
595 lower_ptr = &lower->values;
596 upper_ptr = &upper->values;
597 for (i = 0; i < MAX_MACHINE_MODE; i++)
599 copy = xstrdup (GET_MODE_NAME (i));
600 for (p = copy; *p != 0; p++)
603 upper_ptr = add_map_value (upper_ptr, i, GET_MODE_NAME (i));
604 lower_ptr = add_map_value (lower_ptr, i, copy);
607 lower = add_mapping (&codes, codes.attrs, "code", 0);
608 upper = add_mapping (&codes, codes.attrs, "CODE", 0);
609 lower_ptr = &lower->values;
610 upper_ptr = &upper->values;
611 for (i = 0; i < NUM_RTX_CODE; i++)
613 copy = xstrdup (GET_RTX_NAME (i));
614 for (p = copy; *p != 0; p++)
617 lower_ptr = add_map_value (lower_ptr, i, GET_RTX_NAME (i));
618 upper_ptr = add_map_value (upper_ptr, i, copy);
622 /* Read an rtx code name into the buffer STR[].
623 It is terminated by any of the punctuation chars of rtx printed syntax. */
626 read_name (char *str, FILE *infile)
631 c = read_skip_spaces (infile);
636 if (c == ' ' || c == '\n' || c == '\t' || c == '\f' || c == '\r' || c == EOF)
638 if (c == ':' || c == ')' || c == ']' || c == '"' || c == '/'
639 || c == '(' || c == '[')
648 fatal_with_file_and_line (infile, "missing name or number");
656 /* Do constant expansion. */
657 struct md_constant *def;
662 struct md_constant tmp_def;
665 def = (struct md_constant *) htab_find (md_constants, &tmp_def);
674 /* Provide a version of a function to read a long long if the system does
676 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !defined(HAVE_ATOLL) && !defined(HAVE_ATOQ)
677 HOST_WIDE_INT atoll (const char *);
680 atoll (const char *p)
683 HOST_WIDE_INT tmp_wide;
695 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
696 if (new_wide < tmp_wide)
698 /* Return INT_MAX equiv on overflow. */
699 tmp_wide = (~(unsigned HOST_WIDE_INT) 0) >> 1;
707 tmp_wide = -tmp_wide;
712 /* Given an object that starts with a char * name field, return a hash
713 code for its name. */
715 def_hash (const void *def)
718 const char *string = *(const char *const *) def;
720 for (result = i = 0; *string++ != '\0'; i++)
721 result += ((unsigned char) *string << (i % CHAR_BIT));
725 /* Given two objects that start with char * name fields, return true if
726 they have the same name. */
728 def_name_eq_p (const void *def1, const void *def2)
730 return ! strcmp (*(const char *const *) def1,
731 *(const char *const *) def2);
734 /* INFILE is a FILE pointer to read text from. TMP_CHAR is a buffer suitable
735 to read a name or number into. Process a define_constants directive,
736 starting with the optional space after the "define_constants". */
738 read_constants (FILE *infile, char *tmp_char)
743 c = read_skip_spaces (infile);
745 fatal_expected_char (infile, '[', c);
748 defs = htab_create (32, def_hash, def_name_eq_p, (htab_del) 0);
749 /* Disable constant expansion during definition processing. */
751 while ( (c = read_skip_spaces (infile)) != ']')
753 struct md_constant *def;
757 fatal_expected_char (infile, '(', c);
758 def = XNEW (struct md_constant);
759 def->name = tmp_char;
760 read_name (tmp_char, infile);
761 entry_ptr = htab_find_slot (defs, def, INSERT);
763 def->name = xstrdup (tmp_char);
764 c = read_skip_spaces (infile);
766 read_name (tmp_char, infile);
769 def->value = xstrdup (tmp_char);
774 def = (struct md_constant *) *entry_ptr;
775 if (strcmp (def->value, tmp_char))
776 fatal_with_file_and_line (infile,
777 "redefinition of %s, was %s, now %s",
778 def->name, def->value, tmp_char);
780 c = read_skip_spaces (infile);
782 fatal_expected_char (infile, ')', c);
785 c = read_skip_spaces (infile);
787 fatal_expected_char (infile, ')', c);
790 /* For every constant definition, call CALLBACK with two arguments:
791 a pointer a pointer to the constant definition and INFO.
792 Stops when CALLBACK returns zero. */
794 traverse_md_constants (htab_trav callback, void *info)
797 htab_traverse (md_constants, callback, info);
800 /* INFILE is a FILE pointer to read text from. TMP_CHAR is a buffer
801 suitable to read a name or number into. Process a
802 define_conditions directive, starting with the optional space after
803 the "define_conditions". The directive looks like this:
811 It's not intended to appear in machine descriptions. It is
812 generated by (the program generated by) genconditions.c, and
813 slipped in at the beginning of the sequence of MD files read by
814 most of the other generators. */
816 read_conditions (FILE *infile, char *tmp_char)
820 c = read_skip_spaces (infile);
822 fatal_expected_char (infile, '[', c);
824 while ( (c = read_skip_spaces (infile)) != ']')
830 fatal_expected_char (infile, '(', c);
832 read_name (tmp_char, infile);
833 validate_const_int (infile, tmp_char);
834 value = atoi (tmp_char);
836 c = read_skip_spaces (infile);
838 fatal_expected_char (infile, '"', c);
839 expr = read_quoted_string (infile);
841 c = read_skip_spaces (infile);
843 fatal_expected_char (infile, ')', c);
845 add_c_test (expr, value);
847 c = read_skip_spaces (infile);
849 fatal_expected_char (infile, ')', c);
853 validate_const_int (FILE *infile, const char *string)
859 while (*cp && ISSPACE (*cp))
861 if (*cp == '-' || *cp == '+')
869 fatal_with_file_and_line (infile, "invalid decimal constant \"%s\"\n", string);
872 /* Search GROUP for a mode or code called NAME and return its numerical
873 identifier. INFILE is the file that contained NAME. */
876 find_iterator (struct iterator_group *group, const char *name, FILE *infile)
880 m = (struct mapping *) htab_find (group->iterators, &name);
882 return m->index + group->num_builtins;
883 return group->find_builtin (name, infile);
886 /* Finish reading a declaration of the form:
888 (define... <name> [<value1> ... <valuen>])
890 from INFILE, where each <valuei> is either a bare symbol name or a
891 "(<name> <string>)" pair. The "(define..." part has already been read.
893 Represent the declaration as a "mapping" structure; add it to TABLE
894 (which belongs to GROUP) and return it. */
896 static struct mapping *
897 read_mapping (struct iterator_group *group, htab_t table, FILE *infile)
901 struct map_value **end_ptr;
905 /* Read the mapping name and create a structure for it. */
906 read_name (tmp_char, infile);
907 m = add_mapping (group, table, tmp_char, infile);
909 c = read_skip_spaces (infile);
911 fatal_expected_char (infile, '[', c);
913 /* Read each value. */
914 end_ptr = &m->values;
915 c = read_skip_spaces (infile);
920 /* A bare symbol name that is implicitly paired to an
923 read_name (tmp_char, infile);
928 /* A "(name string)" pair. */
929 read_name (tmp_char, infile);
930 string = read_string (infile, false);
931 c = read_skip_spaces (infile);
933 fatal_expected_char (infile, ')', c);
935 number = group->find_builtin (tmp_char, infile);
936 end_ptr = add_map_value (end_ptr, number, string);
937 c = read_skip_spaces (infile);
941 c = read_skip_spaces (infile);
943 fatal_expected_char (infile, ')', c);
948 /* Check newly-created code iterator ITERATOR to see whether every code has the
949 same format. Initialize the iterator's entry in bellwether_codes. */
952 check_code_iterator (struct mapping *iterator, FILE *infile)
955 enum rtx_code bellwether;
957 bellwether = (enum rtx_code) iterator->values->number;
958 for (v = iterator->values->next; v != 0; v = v->next)
959 if (strcmp (GET_RTX_FORMAT (bellwether), GET_RTX_FORMAT (v->number)) != 0)
960 fatal_with_file_and_line (infile, "code iterator `%s' combines "
961 "different rtx formats", iterator->name);
963 bellwether_codes = XRESIZEVEC (enum rtx_code, bellwether_codes,
964 iterator->index + 1);
965 bellwether_codes[iterator->index] = bellwether;
968 /* Read an rtx in printed representation from INFILE and store its
969 core representation in *X. Also store the line number of the
970 opening '(' in *LINENO. Return true on success or false if the
971 end of file has been reached.
973 read_rtx is not used in the compiler proper, but rather in
974 the utilities gen*.c that construct C code from machine descriptions. */
977 read_rtx (FILE *infile, rtx *x, int *lineno)
979 static rtx queue_head, queue_next;
980 static int queue_lineno;
983 /* Do one-time initialization. */
987 initialize_iterators ();
988 queue_head = rtx_alloc (EXPR_LIST);
993 struct map_value *mode_maps;
994 struct iterator_traverse_data mtd;
997 c = read_skip_spaces (infile);
1002 queue_lineno = read_md_lineno;
1004 from_file = read_rtx_1 (infile, &mode_maps);
1006 return false; /* This confuses a top level (nil) with end of
1007 file, but a top level (nil) would have
1008 crashed our caller anyway. */
1010 queue_next = queue_head;
1011 XEXP (queue_next, 0) = from_file;
1012 XEXP (queue_next, 1) = 0;
1014 mtd.queue = queue_next;
1015 mtd.mode_maps = mode_maps;
1016 mtd.infile = infile;
1017 mtd.unknown_mode_attr = mode_maps ? mode_maps->string : NULL;
1018 htab_traverse (modes.iterators, apply_iterator_traverse, &mtd);
1019 htab_traverse (codes.iterators, apply_iterator_traverse, &mtd);
1020 if (mtd.unknown_mode_attr)
1021 fatal_with_file_and_line (infile,
1022 "undefined attribute '%s' used for mode",
1023 mtd.unknown_mode_attr);
1026 *x = XEXP (queue_next, 0);
1027 *lineno = queue_lineno;
1028 queue_next = XEXP (queue_next, 1);
1033 /* Subroutine of read_rtx that reads one construct from INFILE but
1034 doesn't apply any iterators. */
1037 read_rtx_1 (FILE *infile, struct map_value **mode_maps)
1040 RTX_CODE real_code, bellwether_code;
1041 const char *format_ptr;
1042 /* tmp_char is a buffer used for reading decimal integers
1043 and names of rtx types and machine modes.
1044 Therefore, 256 must be enough. */
1049 HOST_WIDE_INT tmp_wide;
1051 /* Linked list structure for making RTXs: */
1054 struct rtx_list *next;
1055 rtx value; /* Value of this node. */
1059 c = read_skip_spaces (infile); /* Should be open paren. */
1065 fatal_expected_char (infile, '(', c);
1067 read_name (tmp_char, infile);
1068 if (strcmp (tmp_char, "nil") == 0)
1070 /* (nil) stands for an expression that isn't there. */
1071 c = read_skip_spaces (infile);
1073 fatal_expected_char (infile, ')', c);
1076 if (strcmp (tmp_char, "define_constants") == 0)
1078 read_constants (infile, tmp_char);
1081 if (strcmp (tmp_char, "define_conditions") == 0)
1083 read_conditions (infile, tmp_char);
1086 if (strcmp (tmp_char, "define_mode_attr") == 0)
1088 read_mapping (&modes, modes.attrs, infile);
1091 if (strcmp (tmp_char, "define_mode_iterator") == 0)
1093 read_mapping (&modes, modes.iterators, infile);
1096 if (strcmp (tmp_char, "define_code_attr") == 0)
1098 read_mapping (&codes, codes.attrs, infile);
1101 if (strcmp (tmp_char, "define_code_iterator") == 0)
1103 check_code_iterator (read_mapping (&codes, codes.iterators, infile),
1107 real_code = (enum rtx_code) find_iterator (&codes, tmp_char, infile);
1108 bellwether_code = BELLWETHER_CODE (real_code);
1110 /* If we end up with an insn expression then we free this space below. */
1111 return_rtx = rtx_alloc (bellwether_code);
1112 format_ptr = GET_RTX_FORMAT (bellwether_code);
1113 PUT_CODE (return_rtx, real_code);
1115 /* If what follows is `: mode ', read it and
1116 store the mode in the rtx. */
1118 i = read_skip_spaces (infile);
1123 read_name (tmp_char, infile);
1124 if (tmp_char[0] != '<' || tmp_char[strlen (tmp_char) - 1] != '>')
1125 mode = find_iterator (&modes, tmp_char, infile);
1127 mode = mode_attr_index (mode_maps, tmp_char);
1128 PUT_MODE (return_rtx, (enum machine_mode) mode);
1129 if (GET_MODE (return_rtx) != mode)
1130 fatal_with_file_and_line (infile, "mode too large");
1135 for (i = 0; format_ptr[i] != 0; i++)
1136 switch (format_ptr[i])
1138 /* 0 means a field for internal use only.
1139 Don't expect it to be present in the input. */
1145 XEXP (return_rtx, i) = read_rtx_1 (infile, mode_maps);
1149 /* 'V' is an optional vector: if a closeparen follows,
1150 just store NULL for this element. */
1151 c = read_skip_spaces (infile);
1155 XVEC (return_rtx, i) = 0;
1158 /* Now process the vector. */
1162 /* Obstack to store scratch vector in. */
1163 struct obstack vector_stack;
1164 int list_counter = 0;
1165 rtvec return_vec = NULL_RTVEC;
1167 c = read_skip_spaces (infile);
1169 fatal_expected_char (infile, '[', c);
1171 /* Add expressions to a list, while keeping a count. */
1172 obstack_init (&vector_stack);
1173 while ((c = read_skip_spaces (infile)) && c != ']')
1176 fatal_expected_char (infile, ']', c);
1179 obstack_ptr_grow (&vector_stack, read_rtx_1 (infile, mode_maps));
1181 if (list_counter > 0)
1183 return_vec = rtvec_alloc (list_counter);
1184 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
1185 list_counter * sizeof (rtx));
1187 else if (format_ptr[i] == 'E')
1188 fatal_with_file_and_line (infile,
1189 "vector must have at least one element");
1190 XVEC (return_rtx, i) = return_vec;
1191 obstack_free (&vector_stack, NULL);
1192 /* close bracket gotten */
1203 c = read_skip_spaces (infile);
1207 /* 'S' fields are optional and should be NULL if no string
1208 was given. Also allow normal 's' and 'T' strings to be
1209 omitted, treating them in the same way as empty strings. */
1210 XSTR (return_rtx, i) = (format_ptr[i] == 'S' ? NULL : "");
1214 /* The output template slot of a DEFINE_INSN,
1215 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
1216 gets a star inserted as its first character, if it is
1217 written with a brace block instead of a string constant. */
1218 star_if_braced = (format_ptr[i] == 'T');
1220 stringbuf = read_string (infile, star_if_braced);
1222 /* For insn patterns, we want to provide a default name
1223 based on the file and line, like "*foo.md:12", if the
1224 given name is blank. These are only for define_insn and
1225 define_insn_and_split, to aid debugging. */
1226 if (*stringbuf == '\0'
1228 && (GET_CODE (return_rtx) == DEFINE_INSN
1229 || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT))
1232 const char *fn = (read_md_filename ? read_md_filename : "rtx");
1234 for (slash = fn; *slash; slash ++)
1235 if (*slash == '/' || *slash == '\\' || *slash == ':')
1237 obstack_1grow (&string_obstack, '*');
1238 obstack_grow (&string_obstack, fn, strlen (fn));
1239 sprintf (line_name, ":%d", read_md_lineno);
1240 obstack_grow (&string_obstack, line_name, strlen (line_name)+1);
1241 stringbuf = XOBFINISH (&string_obstack, char *);
1245 XTMPL (return_rtx, i) = stringbuf;
1247 XSTR (return_rtx, i) = stringbuf;
1252 read_name (tmp_char, infile);
1253 validate_const_int (infile, tmp_char);
1254 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1255 tmp_wide = atoi (tmp_char);
1257 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1258 tmp_wide = atol (tmp_char);
1260 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1261 But prefer not to use our hand-rolled function above either. */
1262 #if defined(HAVE_ATOLL) || !defined(HAVE_ATOQ)
1263 tmp_wide = atoll (tmp_char);
1265 tmp_wide = atoq (tmp_char);
1269 XWINT (return_rtx, i) = tmp_wide;
1274 read_name (tmp_char, infile);
1275 validate_const_int (infile, tmp_char);
1276 tmp_int = atoi (tmp_char);
1277 XINT (return_rtx, i) = tmp_int;
1284 c = read_skip_spaces (infile);
1287 /* Syntactic sugar for AND and IOR, allowing Lisp-like
1288 arbitrary number of arguments for them. */
1289 if (c == '(' && (GET_CODE (return_rtx) == AND
1290 || GET_CODE (return_rtx) == IOR))
1291 return read_rtx_variadic (infile, mode_maps, return_rtx);
1293 fatal_expected_char (infile, ')', c);
1299 /* Mutually recursive subroutine of read_rtx which reads
1300 (thing x1 x2 x3 ...) and produces RTL as if
1301 (thing x1 (thing x2 (thing x3 ...))) had been written.
1302 When called, FORM is (thing x1 x2), and the file position
1303 is just past the leading parenthesis of x3. Only works
1304 for THINGs which are dyadic expressions, e.g. AND, IOR. */
1306 read_rtx_variadic (FILE *infile, struct map_value **mode_maps, rtx form)
1315 q = rtx_alloc (GET_CODE (p));
1316 PUT_MODE (q, GET_MODE (p));
1318 XEXP (q, 0) = XEXP (p, 1);
1319 XEXP (q, 1) = read_rtx_1 (infile, mode_maps);
1323 c = read_skip_spaces (infile);
1328 fatal_expected_char (infile, ')', c);