* config/stormy16/stormy16.c (xstormy16_asm_output_aligned_common):
[official-gcc.git] / gcc / read-rtl.c
blob2cd4e22a4c1d9b17d3c1c9028a8415f08d3adea5
1 /* RTL reader for GCC.
2 Copyright (C) 1987, 1988, 1991, 1994, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004
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
11 version.
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
16 for more details.
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
21 02111-1307, USA. */
23 #include "bconfig.h"
25 /* Disable rtl checking; it conflicts with the macro handling. */
26 #undef ENABLE_RTL_CHECKING
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "rtl.h"
32 #include "obstack.h"
33 #include "hashtab.h"
35 static htab_t md_constants;
37 /* One element in a singly-linked list of (integer, string) pairs. */
38 struct map_value {
39 struct map_value *next;
40 int number;
41 const char *string;
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. */
47 struct mapping {
48 /* The name of the macro or attribute. */
49 const char *name;
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. */
56 int index;
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. */
63 struct macro_group {
64 /* Tables of "mapping" structures, one for attributes and one for macros. */
65 htab_t attrs, macros;
67 /* The number of "real" modes or codes (and by extension, the first
68 number available for use as a macro placeholder). */
69 int num_builtins;
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 /* If CODE is the number of a code macro, return a real rtx code that
83 has the same format. Return CODE otherwise. */
84 #define BELLWETHER_CODE(CODE) \
85 ((CODE) < NUM_RTX_CODE ? CODE : bellwether_codes[CODE - NUM_RTX_CODE])
87 static void fatal_with_file_and_line (FILE *, const char *, ...)
88 ATTRIBUTE_PRINTF_2 ATTRIBUTE_NORETURN;
89 static void fatal_expected_char (FILE *, int, int) ATTRIBUTE_NORETURN;
90 static int find_mode (const char *, FILE *);
91 static bool uses_mode_macro_p (rtx, int);
92 static void apply_mode_macro (rtx, int);
93 static int find_code (const char *, FILE *);
94 static bool uses_code_macro_p (rtx, int);
95 static void apply_code_macro (rtx, int);
96 static const char *apply_macro_to_string (const char *, struct mapping *, int);
97 static rtx apply_macro_to_rtx (rtx, struct mapping *, int);
98 static bool uses_macro_p (rtx, struct mapping *);
99 static const char *add_condition_to_string (const char *, const char *);
100 static void add_condition_to_rtx (rtx, const char *);
101 static int apply_macro_traverse (void **, void *);
102 static struct mapping *add_mapping (struct macro_group *, htab_t t,
103 const char *, FILE *);
104 static struct map_value **add_map_value (struct map_value **,
105 int, const char *);
106 static void initialize_macros (void);
107 static void read_name (char *, FILE *);
108 static char *read_string (FILE *, int);
109 static char *read_quoted_string (FILE *);
110 static char *read_braced_string (FILE *);
111 static void read_escape (FILE *);
112 static hashval_t def_hash (const void *);
113 static int def_name_eq_p (const void *, const void *);
114 static void read_constants (FILE *infile, char *tmp_char);
115 static void validate_const_int (FILE *, const char *);
116 static int find_macro (struct macro_group *, const char *, FILE *);
117 static struct mapping *read_mapping (struct macro_group *, htab_t, FILE *);
118 static void check_code_macro (struct mapping *, FILE *);
119 static rtx read_rtx_1 (FILE *);
121 /* The mode and code macro structures. */
122 static struct macro_group modes, codes;
124 /* Index I is the value of BELLWETHER_CODE (I + NUM_RTX_CODE). */
125 static enum rtx_code *bellwether_codes;
127 /* Obstack used for allocating RTL strings. */
128 static struct obstack string_obstack;
130 /* Subroutines of read_rtx. */
132 /* The current line number for the file. */
133 int read_rtx_lineno = 1;
135 /* The filename for aborting with file and line. */
136 const char *read_rtx_filename = "<unknown>";
138 static void
139 fatal_with_file_and_line (FILE *infile, const char *msg, ...)
141 char context[64];
142 size_t i;
143 int c;
144 va_list ap;
146 va_start (ap, msg);
148 fprintf (stderr, "%s:%d: ", read_rtx_filename, read_rtx_lineno);
149 vfprintf (stderr, msg, ap);
150 putc ('\n', stderr);
152 /* Gather some following context. */
153 for (i = 0; i < sizeof (context)-1; ++i)
155 c = getc (infile);
156 if (c == EOF)
157 break;
158 if (c == '\r' || c == '\n')
159 break;
160 context[i] = c;
162 context[i] = '\0';
164 fprintf (stderr, "%s:%d: following context is `%s'\n",
165 read_rtx_filename, read_rtx_lineno, context);
167 va_end (ap);
168 exit (1);
171 /* Dump code after printing a message. Used when read_rtx finds
172 invalid data. */
174 static void
175 fatal_expected_char (FILE *infile, int expected_c, int actual_c)
177 fatal_with_file_and_line (infile, "expected character `%c', found `%c'",
178 expected_c, actual_c);
181 /* Implementations of the macro_group callbacks for modes. */
183 static int
184 find_mode (const char *name, FILE *infile)
186 int i;
188 for (i = 0; i < NUM_MACHINE_MODES; i++)
189 if (strcmp (GET_MODE_NAME (i), name) == 0)
190 return i;
192 fatal_with_file_and_line (infile, "unknown mode `%s'", name);
195 static bool
196 uses_mode_macro_p (rtx x, int mode)
198 return (int) GET_MODE (x) == mode;
201 static void
202 apply_mode_macro (rtx x, int mode)
204 PUT_MODE (x, mode);
207 /* Implementations of the macro_group callbacks for codes. */
209 static int
210 find_code (const char *name, FILE *infile)
212 int i;
214 for (i = 0; i < NUM_RTX_CODE; i++)
215 if (strcmp (GET_RTX_NAME (i), name) == 0)
216 return i;
218 fatal_with_file_and_line (infile, "unknown rtx code `%s'", name);
221 static bool
222 uses_code_macro_p (rtx x, int code)
224 return (int) GET_CODE (x) == code;
227 static void
228 apply_code_macro (rtx x, int code)
230 PUT_CODE (x, code);
233 /* Given that MACRO is being expanded as VALUE, apply the appropriate
234 string substitutions to STRING. Return the new string if any changes
235 were needed, otherwise return STRING itself. */
237 static const char *
238 apply_macro_to_string (const char *string, struct mapping *macro, int value)
240 char *base, *copy, *p, *attr, *start, *end;
241 struct mapping *m;
242 struct map_value *v;
244 if (string == 0)
245 return string;
247 base = p = copy = ASTRDUP (string);
248 while ((start = index (p, '<')) && (end = index (start, '>')))
250 p = start + 1;
252 /* If there's a "macro:" prefix, check whether the macro name matches.
253 Set ATTR to the start of the attribute name. */
254 attr = index (p, ':');
255 if (attr == 0 || attr > end)
256 attr = p;
257 else
259 if (strncmp (p, macro->name, attr - p) != 0
260 || macro->name[attr - p] != 0)
261 continue;
262 attr++;
265 /* Find the attribute specification. */
266 *end = 0;
267 m = (struct mapping *) htab_find (macro->group->attrs, &attr);
268 *end = '>';
269 if (m == 0)
270 continue;
272 /* Find the attribute value for VALUE. */
273 for (v = m->values; v != 0; v = v->next)
274 if (v->number == value)
275 break;
276 if (v == 0)
277 continue;
279 /* Add everything between the last copied byte and the '<',
280 then add in the attribute value. */
281 obstack_grow (&string_obstack, base, start - base);
282 obstack_grow (&string_obstack, v->string, strlen (v->string));
283 base = end + 1;
285 if (base != copy)
287 obstack_grow (&string_obstack, base, strlen (base) + 1);
288 return (char *) obstack_finish (&string_obstack);
290 return string;
293 /* Return a copy of ORIGINAL in which all uses of MACRO have been
294 replaced by VALUE. */
296 static rtx
297 apply_macro_to_rtx (rtx original, struct mapping *macro, int value)
299 struct macro_group *group;
300 const char *format_ptr;
301 int i, j;
302 rtx x;
303 enum rtx_code bellwether_code;
305 if (original == 0)
306 return original;
308 /* Create a shallow copy of ORIGINAL. */
309 bellwether_code = BELLWETHER_CODE (GET_CODE (original));
310 x = rtx_alloc (bellwether_code);
311 memcpy (x, original, RTX_SIZE (bellwether_code));
313 /* Change the mode or code itself. */
314 group = macro->group;
315 if (group->uses_macro_p (x, macro->index + group->num_builtins))
316 group->apply_macro (x, value);
318 /* Change each string and recursively change each rtx. */
319 format_ptr = GET_RTX_FORMAT (bellwether_code);
320 for (i = 0; format_ptr[i] != 0; i++)
321 switch (format_ptr[i])
323 case 'T':
324 XTMPL (x, i) = apply_macro_to_string (XTMPL (x, i), macro, value);
325 break;
327 case 'S':
328 case 's':
329 XSTR (x, i) = apply_macro_to_string (XSTR (x, i), macro, value);
330 break;
332 case 'e':
333 XEXP (x, i) = apply_macro_to_rtx (XEXP (x, i), macro, value);
334 break;
336 case 'V':
337 case 'E':
338 if (XVEC (original, i))
340 XVEC (x, i) = rtvec_alloc (XVECLEN (original, i));
341 for (j = 0; j < XVECLEN (x, i); j++)
342 XVECEXP (x, i, j) = apply_macro_to_rtx (XVECEXP (original, i, j),
343 macro, value);
345 break;
347 default:
348 break;
350 return x;
353 /* Return true if X (or some subexpression of X) uses macro MACRO. */
355 static bool
356 uses_macro_p (rtx x, struct mapping *macro)
358 struct macro_group *group;
359 const char *format_ptr;
360 int i, j;
362 if (x == 0)
363 return false;
365 group = macro->group;
366 if (group->uses_macro_p (x, macro->index + group->num_builtins))
367 return true;
369 format_ptr = GET_RTX_FORMAT (BELLWETHER_CODE (GET_CODE (x)));
370 for (i = 0; format_ptr[i] != 0; i++)
371 switch (format_ptr[i])
373 case 'e':
374 if (uses_macro_p (XEXP (x, i), macro))
375 return true;
376 break;
378 case 'V':
379 case 'E':
380 if (XVEC (x, i))
381 for (j = 0; j < XVECLEN (x, i); j++)
382 if (uses_macro_p (XVECEXP (x, i, j), macro))
383 return true;
384 break;
386 default:
387 break;
389 return false;
392 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
393 has the form "&& ..." (as used in define_insn_and_splits), assume that
394 EXTRA is already satisfied. Empty strings are treated like "true". */
396 static const char *
397 add_condition_to_string (const char *original, const char *extra)
399 char *result;
401 if (original == 0 || original[0] == 0)
402 return extra;
404 if ((original[0] == '&' && original[1] == '&') || extra[0] == 0)
405 return original;
407 asprintf (&result, "(%s) && (%s)", original, extra);
408 return result;
411 /* Like add_condition, but applied to all conditions in rtx X. */
413 static void
414 add_condition_to_rtx (rtx x, const char *extra)
416 switch (GET_CODE (x))
418 case DEFINE_INSN:
419 case DEFINE_EXPAND:
420 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
421 break;
423 case DEFINE_SPLIT:
424 case DEFINE_PEEPHOLE:
425 case DEFINE_PEEPHOLE2:
426 case DEFINE_COND_EXEC:
427 XSTR (x, 1) = add_condition_to_string (XSTR (x, 1), extra);
428 break;
430 case DEFINE_INSN_AND_SPLIT:
431 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
432 XSTR (x, 4) = add_condition_to_string (XSTR (x, 4), extra);
433 break;
435 default:
436 break;
440 /* A htab_traverse callback. Search the EXPR_LIST given by DATA
441 for rtxes that use the macro in *SLOT. Replace each such rtx
442 with a list of expansions. */
444 static int
445 apply_macro_traverse (void **slot, void *data)
447 struct mapping *macro;
448 struct map_value *v;
449 rtx elem, new_elem, original, x;
451 macro = (struct mapping *) *slot;
452 for (elem = (rtx) data; elem != 0; elem = XEXP (elem, 1))
453 if (uses_macro_p (XEXP (elem, 0), macro))
455 original = XEXP (elem, 0);
456 for (v = macro->values; v != 0; v = v->next)
458 x = apply_macro_to_rtx (original, macro, v->number);
459 add_condition_to_rtx (x, v->string);
460 if (v != macro->values)
462 /* Insert a new EXPR_LIST node after ELEM and put the
463 new expansion there. */
464 new_elem = rtx_alloc (EXPR_LIST);
465 XEXP (new_elem, 1) = XEXP (elem, 1);
466 XEXP (elem, 1) = new_elem;
467 elem = new_elem;
469 XEXP (elem, 0) = x;
472 return 1;
475 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
476 of the mapping, GROUP is the group to which it belongs, and INFILE
477 is the file that defined the mapping. */
479 static struct mapping *
480 add_mapping (struct macro_group *group, htab_t table,
481 const char *name, FILE *infile)
483 struct mapping *m;
484 void **slot;
486 m = XNEW (struct mapping);
487 m->name = xstrdup (name);
488 m->group = group;
489 m->index = htab_elements (table);
490 m->values = 0;
492 slot = htab_find_slot (table, m, INSERT);
493 if (*slot != 0)
494 fatal_with_file_and_line (infile, "`%s' already defined", name);
496 *slot = m;
497 return m;
500 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
501 END_PTR points to the current null terminator for the list; return
502 a pointer the new null terminator. */
504 static struct map_value **
505 add_map_value (struct map_value **end_ptr, int number, const char *string)
507 struct map_value *value;
509 value = XNEW (struct map_value);
510 value->next = 0;
511 value->number = number;
512 value->string = string;
514 *end_ptr = value;
515 return &value->next;
518 /* Do one-time initialization of the mode and code attributes. */
520 static void
521 initialize_macros (void)
523 struct mapping *lower, *upper;
524 struct map_value **lower_ptr, **upper_ptr;
525 char *copy, *p;
526 int i;
528 modes.attrs = htab_create (13, def_hash, def_name_eq_p, 0);
529 modes.macros = htab_create (13, def_hash, def_name_eq_p, 0);
530 modes.num_builtins = MAX_MACHINE_MODE;
531 modes.find_builtin = find_mode;
532 modes.uses_macro_p = uses_mode_macro_p;
533 modes.apply_macro = apply_mode_macro;
535 codes.attrs = htab_create (13, def_hash, def_name_eq_p, 0);
536 codes.macros = htab_create (13, def_hash, def_name_eq_p, 0);
537 codes.num_builtins = NUM_RTX_CODE;
538 codes.find_builtin = find_code;
539 codes.uses_macro_p = uses_code_macro_p;
540 codes.apply_macro = apply_code_macro;
542 lower = add_mapping (&modes, modes.attrs, "mode", 0);
543 upper = add_mapping (&modes, modes.attrs, "MODE", 0);
544 lower_ptr = &lower->values;
545 upper_ptr = &upper->values;
546 for (i = 0; i < MAX_MACHINE_MODE; i++)
548 copy = xstrdup (GET_MODE_NAME (i));
549 for (p = copy; *p != 0; p++)
550 *p = TOLOWER (*p);
552 upper_ptr = add_map_value (upper_ptr, i, GET_MODE_NAME (i));
553 lower_ptr = add_map_value (lower_ptr, i, copy);
556 lower = add_mapping (&codes, codes.attrs, "code", 0);
557 upper = add_mapping (&codes, codes.attrs, "CODE", 0);
558 lower_ptr = &lower->values;
559 upper_ptr = &upper->values;
560 for (i = 0; i < NUM_RTX_CODE; i++)
562 copy = xstrdup (GET_RTX_NAME (i));
563 for (p = copy; *p != 0; p++)
564 *p = TOUPPER (*p);
566 lower_ptr = add_map_value (lower_ptr, i, GET_RTX_NAME (i));
567 upper_ptr = add_map_value (upper_ptr, i, copy);
571 /* Read chars from INFILE until a non-whitespace char
572 and return that. Comments, both Lisp style and C style,
573 are treated as whitespace.
574 Tools such as genflags use this function. */
577 read_skip_spaces (FILE *infile)
579 int c;
581 while (1)
583 c = getc (infile);
584 switch (c)
586 case '\n':
587 read_rtx_lineno++;
588 break;
590 case ' ': case '\t': case '\f': case '\r':
591 break;
593 case ';':
595 c = getc (infile);
596 while (c != '\n' && c != EOF);
597 read_rtx_lineno++;
598 break;
600 case '/':
602 int prevc;
603 c = getc (infile);
604 if (c != '*')
605 fatal_expected_char (infile, '*', c);
607 prevc = 0;
608 while ((c = getc (infile)) && c != EOF)
610 if (c == '\n')
611 read_rtx_lineno++;
612 else if (prevc == '*' && c == '/')
613 break;
614 prevc = c;
617 break;
619 default:
620 return c;
625 /* Read an rtx code name into the buffer STR[].
626 It is terminated by any of the punctuation chars of rtx printed syntax. */
628 static void
629 read_name (char *str, FILE *infile)
631 char *p;
632 int c;
634 c = read_skip_spaces (infile);
636 p = str;
637 while (1)
639 if (c == ' ' || c == '\n' || c == '\t' || c == '\f' || c == '\r')
640 break;
641 if (c == ':' || c == ')' || c == ']' || c == '"' || c == '/'
642 || c == '(' || c == '[')
644 ungetc (c, infile);
645 break;
647 *p++ = c;
648 c = getc (infile);
650 if (p == str)
651 fatal_with_file_and_line (infile, "missing name or number");
652 if (c == '\n')
653 read_rtx_lineno++;
655 *p = 0;
657 if (md_constants)
659 /* Do constant expansion. */
660 struct md_constant *def;
662 p = str;
665 struct md_constant tmp_def;
667 tmp_def.name = p;
668 def = (struct md_constant *) htab_find (md_constants, &tmp_def);
669 if (def)
670 p = def->value;
671 } while (def);
672 if (p != str)
673 strcpy (str, p);
677 /* Subroutine of the string readers. Handles backslash escapes.
678 Caller has read the backslash, but not placed it into the obstack. */
679 static void
680 read_escape (FILE *infile)
682 int c = getc (infile);
684 switch (c)
686 /* Backslash-newline is replaced by nothing, as in C. */
687 case '\n':
688 read_rtx_lineno++;
689 return;
691 /* \" \' \\ are replaced by the second character. */
692 case '\\':
693 case '"':
694 case '\'':
695 break;
697 /* Standard C string escapes:
698 \a \b \f \n \r \t \v
699 \[0-7] \x
700 all are passed through to the output string unmolested.
701 In normal use these wind up in a string constant processed
702 by the C compiler, which will translate them appropriately.
703 We do not bother checking that \[0-7] are followed by up to
704 two octal digits, or that \x is followed by N hex digits.
705 \? \u \U are left out because they are not in traditional C. */
706 case 'a': case 'b': case 'f': case 'n': case 'r': case 't': case 'v':
707 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
708 case '7': case 'x':
709 obstack_1grow (&string_obstack, '\\');
710 break;
712 /* \; makes stuff for a C string constant containing
713 newline and tab. */
714 case ';':
715 obstack_grow (&string_obstack, "\\n\\t", 4);
716 return;
718 /* pass anything else through, but issue a warning. */
719 default:
720 fprintf (stderr, "%s:%d: warning: unrecognized escape \\%c\n",
721 read_rtx_filename, read_rtx_lineno, c);
722 obstack_1grow (&string_obstack, '\\');
723 break;
726 obstack_1grow (&string_obstack, c);
730 /* Read a double-quoted string onto the obstack. Caller has scanned
731 the leading quote. */
732 static char *
733 read_quoted_string (FILE *infile)
735 int c;
737 while (1)
739 c = getc (infile); /* Read the string */
740 if (c == '\n')
741 read_rtx_lineno++;
742 else if (c == '\\')
744 read_escape (infile);
745 continue;
747 else if (c == '"')
748 break;
750 obstack_1grow (&string_obstack, c);
753 obstack_1grow (&string_obstack, 0);
754 return (char *) obstack_finish (&string_obstack);
757 /* Read a braced string (a la Tcl) onto the string obstack. Caller
758 has scanned the leading brace. Note that unlike quoted strings,
759 the outermost braces _are_ included in the string constant. */
760 static char *
761 read_braced_string (FILE *infile)
763 int c;
764 int brace_depth = 1; /* caller-processed */
765 unsigned long starting_read_rtx_lineno = read_rtx_lineno;
767 obstack_1grow (&string_obstack, '{');
768 while (brace_depth)
770 c = getc (infile); /* Read the string */
772 if (c == '\n')
773 read_rtx_lineno++;
774 else if (c == '{')
775 brace_depth++;
776 else if (c == '}')
777 brace_depth--;
778 else if (c == '\\')
780 read_escape (infile);
781 continue;
783 else if (c == EOF)
784 fatal_with_file_and_line
785 (infile, "missing closing } for opening brace on line %lu",
786 starting_read_rtx_lineno);
788 obstack_1grow (&string_obstack, c);
791 obstack_1grow (&string_obstack, 0);
792 return (char *) obstack_finish (&string_obstack);
795 /* Read some kind of string constant. This is the high-level routine
796 used by read_rtx. It handles surrounding parentheses, leading star,
797 and dispatch to the appropriate string constant reader. */
799 static char *
800 read_string (FILE *infile, int star_if_braced)
802 char *stringbuf;
803 int saw_paren = 0;
804 int c;
806 c = read_skip_spaces (infile);
807 if (c == '(')
809 saw_paren = 1;
810 c = read_skip_spaces (infile);
813 if (c == '"')
814 stringbuf = read_quoted_string (infile);
815 else if (c == '{')
817 if (star_if_braced)
818 obstack_1grow (&string_obstack, '*');
819 stringbuf = read_braced_string (infile);
821 else
822 fatal_with_file_and_line (infile, "expected `\"' or `{', found `%c'", c);
824 if (saw_paren)
826 c = read_skip_spaces (infile);
827 if (c != ')')
828 fatal_expected_char (infile, ')', c);
831 return stringbuf;
834 /* Provide a version of a function to read a long long if the system does
835 not provide one. */
836 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !defined(HAVE_ATOLL) && !defined(HAVE_ATOQ)
837 HOST_WIDE_INT atoll (const char *);
839 HOST_WIDE_INT
840 atoll (const char *p)
842 int neg = 0;
843 HOST_WIDE_INT tmp_wide;
845 while (ISSPACE (*p))
846 p++;
847 if (*p == '-')
848 neg = 1, p++;
849 else if (*p == '+')
850 p++;
852 tmp_wide = 0;
853 while (ISDIGIT (*p))
855 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
856 if (new_wide < tmp_wide)
858 /* Return INT_MAX equiv on overflow. */
859 tmp_wide = (~(unsigned HOST_WIDE_INT) 0) >> 1;
860 break;
862 tmp_wide = new_wide;
863 p++;
866 if (neg)
867 tmp_wide = -tmp_wide;
868 return tmp_wide;
870 #endif
872 /* Given an object that starts with a char * name field, return a hash
873 code for its name. */
874 static hashval_t
875 def_hash (const void *def)
877 unsigned result, i;
878 const char *string = *(const char *const *) def;
880 for (result = i = 0; *string++ != '\0'; i++)
881 result += ((unsigned char) *string << (i % CHAR_BIT));
882 return result;
885 /* Given two objects that start with char * name fields, return true if
886 they have the same name. */
887 static int
888 def_name_eq_p (const void *def1, const void *def2)
890 return ! strcmp (*(const char *const *) def1,
891 *(const char *const *) def2);
894 /* INFILE is a FILE pointer to read text from. TMP_CHAR is a buffer suitable
895 to read a name or number into. Process a define_constants directive,
896 starting with the optional space after the "define_constants". */
897 static void
898 read_constants (FILE *infile, char *tmp_char)
900 int c;
901 htab_t defs;
903 c = read_skip_spaces (infile);
904 if (c != '[')
905 fatal_expected_char (infile, '[', c);
906 defs = md_constants;
907 if (! defs)
908 defs = htab_create (32, def_hash, def_name_eq_p, (htab_del) 0);
909 /* Disable constant expansion during definition processing. */
910 md_constants = 0;
911 while ( (c = read_skip_spaces (infile)) != ']')
913 struct md_constant *def;
914 void **entry_ptr;
916 if (c != '(')
917 fatal_expected_char (infile, '(', c);
918 def = XNEW (struct md_constant);
919 def->name = tmp_char;
920 read_name (tmp_char, infile);
921 entry_ptr = htab_find_slot (defs, def, INSERT);
922 if (! *entry_ptr)
923 def->name = xstrdup (tmp_char);
924 c = read_skip_spaces (infile);
925 ungetc (c, infile);
926 read_name (tmp_char, infile);
927 if (! *entry_ptr)
929 def->value = xstrdup (tmp_char);
930 *entry_ptr = def;
932 else
934 def = (struct md_constant *) *entry_ptr;
935 if (strcmp (def->value, tmp_char))
936 fatal_with_file_and_line (infile,
937 "redefinition of %s, was %s, now %s",
938 def->name, def->value, tmp_char);
940 c = read_skip_spaces (infile);
941 if (c != ')')
942 fatal_expected_char (infile, ')', c);
944 md_constants = defs;
945 c = read_skip_spaces (infile);
946 if (c != ')')
947 fatal_expected_char (infile, ')', c);
950 /* For every constant definition, call CALLBACK with two arguments:
951 a pointer a pointer to the constant definition and INFO.
952 Stops when CALLBACK returns zero. */
953 void
954 traverse_md_constants (htab_trav callback, void *info)
956 if (md_constants)
957 htab_traverse (md_constants, callback, info);
960 static void
961 validate_const_int (FILE *infile, const char *string)
963 const char *cp;
964 int valid = 1;
966 cp = string;
967 while (*cp && ISSPACE (*cp))
968 cp++;
969 if (*cp == '-' || *cp == '+')
970 cp++;
971 if (*cp == 0)
972 valid = 0;
973 for (; *cp; cp++)
974 if (! ISDIGIT (*cp))
975 valid = 0;
976 if (!valid)
977 fatal_with_file_and_line (infile, "invalid decimal constant \"%s\"\n", string);
980 /* Search GROUP for a mode or code called NAME and return its numerical
981 identifier. INFILE is the file that contained NAME. */
983 static int
984 find_macro (struct macro_group *group, const char *name, FILE *infile)
986 struct mapping *m;
988 m = (struct mapping *) htab_find (group->macros, &name);
989 if (m != 0)
990 return m->index + group->num_builtins;
991 return group->find_builtin (name, infile);
994 /* Finish reading a declaration of the form:
996 (define... <name> [<value1> ... <valuen>])
998 from INFILE, where each <valuei> is either a bare symbol name or a
999 "(<name> <string>)" pair. The "(define..." part has already been read.
1001 Represent the declaration as a "mapping" structure; add it to TABLE
1002 (which belongs to GROUP) and return it. */
1004 static struct mapping *
1005 read_mapping (struct macro_group *group, htab_t table, FILE *infile)
1007 char tmp_char[256];
1008 struct mapping *m;
1009 struct map_value **end_ptr;
1010 const char *string;
1011 int number, c;
1013 /* Read the mapping name and create a structure for it. */
1014 read_name (tmp_char, infile);
1015 m = add_mapping (group, table, tmp_char, infile);
1017 c = read_skip_spaces (infile);
1018 if (c != '[')
1019 fatal_expected_char (infile, '[', c);
1021 /* Read each value. */
1022 end_ptr = &m->values;
1023 c = read_skip_spaces (infile);
1026 if (c != '(')
1028 /* A bare symbol name that is implicitly paired to an
1029 empty string. */
1030 ungetc (c, infile);
1031 read_name (tmp_char, infile);
1032 string = "";
1034 else
1036 /* A "(name string)" pair. */
1037 read_name (tmp_char, infile);
1038 string = read_string (infile, false);
1039 c = read_skip_spaces (infile);
1040 if (c != ')')
1041 fatal_expected_char (infile, ')', c);
1043 number = group->find_builtin (tmp_char, infile);
1044 end_ptr = add_map_value (end_ptr, number, string);
1045 c = read_skip_spaces (infile);
1047 while (c != ']');
1049 c = read_skip_spaces (infile);
1050 if (c != ')')
1051 fatal_expected_char (infile, ')', c);
1053 return m;
1056 /* Check newly-created code macro MACRO to see whether every code has the
1057 same format. Initialize the macro's entry in bellwether_codes. */
1059 static void
1060 check_code_macro (struct mapping *macro, FILE *infile)
1062 struct map_value *v;
1063 enum rtx_code bellwether;
1065 bellwether = macro->values->number;
1066 for (v = macro->values->next; v != 0; v = v->next)
1067 if (strcmp (GET_RTX_FORMAT (bellwether), GET_RTX_FORMAT (v->number)) != 0)
1068 fatal_with_file_and_line (infile, "code macro `%s' combines "
1069 "different rtx formats", macro->name);
1071 bellwether_codes = XRESIZEVEC (enum rtx_code, bellwether_codes,
1072 macro->index + 1);
1073 bellwether_codes[macro->index] = bellwether;
1076 /* Read an rtx in printed representation from INFILE and store its
1077 core representation in *X. Also store the line number of the
1078 opening '(' in *LINENO. Return true on success or false if the
1079 end of file has been reached.
1081 read_rtx is not used in the compiler proper, but rather in
1082 the utilities gen*.c that construct C code from machine descriptions. */
1084 bool
1085 read_rtx (FILE *infile, rtx *x, int *lineno)
1087 static rtx queue_head, queue_next;
1088 static int queue_lineno;
1089 int c;
1091 /* Do one-time initialization. */
1092 if (queue_head == 0)
1094 initialize_macros ();
1095 obstack_init (&string_obstack);
1096 queue_head = rtx_alloc (EXPR_LIST);
1099 if (queue_next == 0)
1101 c = read_skip_spaces (infile);
1102 if (c == EOF)
1103 return false;
1104 ungetc (c, infile);
1106 queue_next = queue_head;
1107 queue_lineno = read_rtx_lineno;
1108 XEXP (queue_next, 0) = read_rtx_1 (infile);
1109 XEXP (queue_next, 1) = 0;
1111 htab_traverse (modes.macros, apply_macro_traverse, queue_next);
1112 htab_traverse (codes.macros, apply_macro_traverse, queue_next);
1115 *x = XEXP (queue_next, 0);
1116 *lineno = queue_lineno;
1117 queue_next = XEXP (queue_next, 1);
1119 return true;
1122 /* Subroutine of read_rtx that reads one construct from INFILE but
1123 doesn't apply any macros. */
1125 static rtx
1126 read_rtx_1 (FILE *infile)
1128 int i;
1129 RTX_CODE real_code, bellwether_code;
1130 const char *format_ptr;
1131 /* tmp_char is a buffer used for reading decimal integers
1132 and names of rtx types and machine modes.
1133 Therefore, 256 must be enough. */
1134 char tmp_char[256];
1135 rtx return_rtx;
1136 int c;
1137 int tmp_int;
1138 HOST_WIDE_INT tmp_wide;
1140 /* Linked list structure for making RTXs: */
1141 struct rtx_list
1143 struct rtx_list *next;
1144 rtx value; /* Value of this node. */
1147 again:
1148 c = read_skip_spaces (infile); /* Should be open paren. */
1149 if (c != '(')
1150 fatal_expected_char (infile, '(', c);
1152 read_name (tmp_char, infile);
1153 if (strcmp (tmp_char, "nil") == 0)
1155 /* (nil) stands for an expression that isn't there. */
1156 c = read_skip_spaces (infile);
1157 if (c != ')')
1158 fatal_expected_char (infile, ')', c);
1159 return 0;
1161 if (strcmp (tmp_char, "define_constants") == 0)
1163 read_constants (infile, tmp_char);
1164 goto again;
1166 if (strcmp (tmp_char, "define_mode_attr") == 0)
1168 read_mapping (&modes, modes.attrs, infile);
1169 goto again;
1171 if (strcmp (tmp_char, "define_mode_macro") == 0)
1173 read_mapping (&modes, modes.macros, infile);
1174 goto again;
1176 if (strcmp (tmp_char, "define_code_attr") == 0)
1178 read_mapping (&codes, codes.attrs, infile);
1179 goto again;
1181 if (strcmp (tmp_char, "define_code_macro") == 0)
1183 check_code_macro (read_mapping (&codes, codes.macros, infile), infile);
1184 goto again;
1186 real_code = find_macro (&codes, tmp_char, infile);
1187 bellwether_code = BELLWETHER_CODE (real_code);
1189 /* If we end up with an insn expression then we free this space below. */
1190 return_rtx = rtx_alloc (bellwether_code);
1191 format_ptr = GET_RTX_FORMAT (bellwether_code);
1192 PUT_CODE (return_rtx, real_code);
1194 /* If what follows is `: mode ', read it and
1195 store the mode in the rtx. */
1197 i = read_skip_spaces (infile);
1198 if (i == ':')
1200 read_name (tmp_char, infile);
1201 PUT_MODE (return_rtx, find_macro (&modes, tmp_char, infile));
1203 else
1204 ungetc (i, infile);
1206 for (i = 0; format_ptr[i] != 0; i++)
1207 switch (format_ptr[i])
1209 /* 0 means a field for internal use only.
1210 Don't expect it to be present in the input. */
1211 case '0':
1212 break;
1214 case 'e':
1215 case 'u':
1216 XEXP (return_rtx, i) = read_rtx_1 (infile);
1217 break;
1219 case 'V':
1220 /* 'V' is an optional vector: if a closeparen follows,
1221 just store NULL for this element. */
1222 c = read_skip_spaces (infile);
1223 ungetc (c, infile);
1224 if (c == ')')
1226 XVEC (return_rtx, i) = 0;
1227 break;
1229 /* Now process the vector. */
1231 case 'E':
1233 /* Obstack to store scratch vector in. */
1234 struct obstack vector_stack;
1235 int list_counter = 0;
1236 rtvec return_vec = NULL_RTVEC;
1238 c = read_skip_spaces (infile);
1239 if (c != '[')
1240 fatal_expected_char (infile, '[', c);
1242 /* Add expressions to a list, while keeping a count. */
1243 obstack_init (&vector_stack);
1244 while ((c = read_skip_spaces (infile)) && c != ']')
1246 ungetc (c, infile);
1247 list_counter++;
1248 obstack_ptr_grow (&vector_stack, read_rtx_1 (infile));
1250 if (list_counter > 0)
1252 return_vec = rtvec_alloc (list_counter);
1253 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
1254 list_counter * sizeof (rtx));
1256 XVEC (return_rtx, i) = return_vec;
1257 obstack_free (&vector_stack, NULL);
1258 /* close bracket gotten */
1260 break;
1262 case 'S':
1263 case 'T':
1264 case 's':
1266 char *stringbuf;
1267 int star_if_braced;
1269 c = read_skip_spaces (infile);
1270 ungetc (c, infile);
1271 if (c == ')')
1273 /* 'S' fields are optional and should be NULL if no string
1274 was given. Also allow normal 's' and 'T' strings to be
1275 omitted, treating them in the same way as empty strings. */
1276 XSTR (return_rtx, i) = (format_ptr[i] == 'S' ? NULL : "");
1277 break;
1280 /* The output template slot of a DEFINE_INSN,
1281 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
1282 gets a star inserted as its first character, if it is
1283 written with a brace block instead of a string constant. */
1284 star_if_braced = (format_ptr[i] == 'T');
1286 stringbuf = read_string (infile, star_if_braced);
1288 /* For insn patterns, we want to provide a default name
1289 based on the file and line, like "*foo.md:12", if the
1290 given name is blank. These are only for define_insn and
1291 define_insn_and_split, to aid debugging. */
1292 if (*stringbuf == '\0'
1293 && i == 0
1294 && (GET_CODE (return_rtx) == DEFINE_INSN
1295 || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT))
1297 char line_name[20];
1298 const char *fn = (read_rtx_filename ? read_rtx_filename : "rtx");
1299 const char *slash;
1300 for (slash = fn; *slash; slash ++)
1301 if (*slash == '/' || *slash == '\\' || *slash == ':')
1302 fn = slash + 1;
1303 obstack_1grow (&string_obstack, '*');
1304 obstack_grow (&string_obstack, fn, strlen (fn));
1305 sprintf (line_name, ":%d", read_rtx_lineno);
1306 obstack_grow (&string_obstack, line_name, strlen (line_name)+1);
1307 stringbuf = (char *) obstack_finish (&string_obstack);
1310 if (star_if_braced)
1311 XTMPL (return_rtx, i) = stringbuf;
1312 else
1313 XSTR (return_rtx, i) = stringbuf;
1315 break;
1317 case 'w':
1318 read_name (tmp_char, infile);
1319 validate_const_int (infile, tmp_char);
1320 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1321 tmp_wide = atoi (tmp_char);
1322 #else
1323 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1324 tmp_wide = atol (tmp_char);
1325 #else
1326 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1327 But prefer not to use our hand-rolled function above either. */
1328 #if defined(HAVE_ATOLL) || !defined(HAVE_ATOQ)
1329 tmp_wide = atoll (tmp_char);
1330 #else
1331 tmp_wide = atoq (tmp_char);
1332 #endif
1333 #endif
1334 #endif
1335 XWINT (return_rtx, i) = tmp_wide;
1336 break;
1338 case 'i':
1339 case 'n':
1340 read_name (tmp_char, infile);
1341 validate_const_int (infile, tmp_char);
1342 tmp_int = atoi (tmp_char);
1343 XINT (return_rtx, i) = tmp_int;
1344 break;
1346 default:
1347 fprintf (stderr,
1348 "switch format wrong in rtl.read_rtx(). format was: %c.\n",
1349 format_ptr[i]);
1350 fprintf (stderr, "\tfile position: %ld\n", ftell (infile));
1351 abort ();
1354 c = read_skip_spaces (infile);
1355 if (c != ')')
1356 fatal_expected_char (infile, ')', c);
1358 return return_rtx;