* arm.h (REVERSE_CONDITION): Define.
[official-gcc.git] / gcc / read-rtl.c
blobdb0b3d7d3fb66ebdf4bfee03dac5111bff3928fe
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"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "obstack.h"
29 #include "hashtab.h"
31 static htab_t md_constants;
33 /* One element in a singly-linked list of (integer, string) pairs. */
34 struct map_value {
35 struct map_value *next;
36 int number;
37 const char *string;
40 /* Maps a macro or attribute name to a list of (integer, string) pairs.
41 The integers are mode or code values; the strings are either C conditions
42 or attribute values. */
43 struct mapping {
44 /* The name of the macro or attribute. */
45 const char *name;
47 /* The group (modes or codes) to which the macro or attribute belongs. */
48 struct macro_group *group;
50 /* Gives a unique number to the attribute or macro. Numbers are
51 allocated consecutively, starting at 0. */
52 int index;
54 /* The list of (integer, string) pairs. */
55 struct map_value *values;
58 /* A structure for abstracting the common parts of code and mode macros. */
59 struct macro_group {
60 /* Tables of "mapping" structures, one for attributes and one for macros. */
61 htab_t attrs, macros;
63 /* The number of "real" modes or codes (and by extension, the first
64 number available for use as a macro placeholder). */
65 int num_builtins;
67 /* Treat the given string as the name of a standard mode or code and
68 return its integer value. Use the given file for error reporting. */
69 int (*find_builtin) (const char *, FILE *);
71 /* Return true if the given rtx uses the given mode or code. */
72 bool (*uses_macro_p) (rtx, int);
74 /* Make the given rtx use the given mode or code. */
75 void (*apply_macro) (rtx, int);
78 /* If CODE is the number of a code macro, return a real rtx code that
79 has the same format. Return CODE otherwise. */
80 #define BELLWETHER_CODE(CODE) \
81 ((CODE) < NUM_RTX_CODE ? CODE : bellwether_codes[CODE - NUM_RTX_CODE])
83 static void fatal_with_file_and_line (FILE *, const char *, ...)
84 ATTRIBUTE_PRINTF_2 ATTRIBUTE_NORETURN;
85 static void fatal_expected_char (FILE *, int, int) ATTRIBUTE_NORETURN;
86 static int find_mode (const char *, FILE *);
87 static bool uses_mode_macro_p (rtx, int);
88 static void apply_mode_macro (rtx, int);
89 static int find_code (const char *, FILE *);
90 static bool uses_code_macro_p (rtx, int);
91 static void apply_code_macro (rtx, int);
92 static const char *apply_macro_to_string (const char *, struct mapping *, int);
93 static rtx apply_macro_to_rtx (rtx, struct mapping *, int);
94 static bool uses_macro_p (rtx, struct mapping *);
95 static const char *add_condition_to_string (const char *, const char *);
96 static void add_condition_to_rtx (rtx, const char *);
97 static int apply_macro_traverse (void **, void *);
98 static struct mapping *add_mapping (struct macro_group *, htab_t t,
99 const char *, FILE *);
100 static struct map_value **add_map_value (struct map_value **,
101 int, const char *);
102 static void initialize_macros (void);
103 static void read_name (char *, FILE *);
104 static char *read_string (FILE *, int);
105 static char *read_quoted_string (FILE *);
106 static char *read_braced_string (FILE *);
107 static void read_escape (FILE *);
108 static hashval_t def_hash (const void *);
109 static int def_name_eq_p (const void *, const void *);
110 static void read_constants (FILE *infile, char *tmp_char);
111 static void validate_const_int (FILE *, const char *);
112 static int find_macro (struct macro_group *, const char *, FILE *);
113 static struct mapping *read_mapping (struct macro_group *, htab_t, FILE *);
114 static void check_code_macro (struct mapping *, FILE *);
115 static rtx read_rtx_1 (FILE *);
117 /* The mode and code macro structures. */
118 static struct macro_group modes, codes;
120 /* Index I is the value of BELLWETHER_CODE (I + NUM_RTX_CODE). */
121 static enum rtx_code *bellwether_codes;
123 /* Obstack used for allocating RTL strings. */
124 static struct obstack string_obstack;
126 /* Subroutines of read_rtx. */
128 /* The current line number for the file. */
129 int read_rtx_lineno = 1;
131 /* The filename for aborting with file and line. */
132 const char *read_rtx_filename = "<unknown>";
134 static void
135 fatal_with_file_and_line (FILE *infile, const char *msg, ...)
137 char context[64];
138 size_t i;
139 int c;
140 va_list ap;
142 va_start (ap, msg);
144 fprintf (stderr, "%s:%d: ", read_rtx_filename, read_rtx_lineno);
145 vfprintf (stderr, msg, ap);
146 putc ('\n', stderr);
148 /* Gather some following context. */
149 for (i = 0; i < sizeof (context)-1; ++i)
151 c = getc (infile);
152 if (c == EOF)
153 break;
154 if (c == '\r' || c == '\n')
155 break;
156 context[i] = c;
158 context[i] = '\0';
160 fprintf (stderr, "%s:%d: following context is `%s'\n",
161 read_rtx_filename, read_rtx_lineno, context);
163 va_end (ap);
164 exit (1);
167 /* Dump code after printing a message. Used when read_rtx finds
168 invalid data. */
170 static void
171 fatal_expected_char (FILE *infile, int expected_c, int actual_c)
173 fatal_with_file_and_line (infile, "expected character `%c', found `%c'",
174 expected_c, actual_c);
177 /* Implementations of the macro_group callbacks for modes. */
179 static int
180 find_mode (const char *name, FILE *infile)
182 int i;
184 for (i = 0; i < NUM_MACHINE_MODES; i++)
185 if (strcmp (GET_MODE_NAME (i), name) == 0)
186 return i;
188 fatal_with_file_and_line (infile, "unknown mode `%s'", name);
191 static bool
192 uses_mode_macro_p (rtx x, int mode)
194 return (int) GET_MODE (x) == mode;
197 static void
198 apply_mode_macro (rtx x, int mode)
200 PUT_MODE (x, mode);
203 /* Implementations of the macro_group callbacks for codes. */
205 static int
206 find_code (const char *name, FILE *infile)
208 int i;
210 for (i = 0; i < NUM_RTX_CODE; i++)
211 if (strcmp (GET_RTX_NAME (i), name) == 0)
212 return i;
214 fatal_with_file_and_line (infile, "unknown rtx code `%s'", name);
217 static bool
218 uses_code_macro_p (rtx x, int code)
220 return (int) GET_CODE (x) == code;
223 static void
224 apply_code_macro (rtx x, int code)
226 PUT_CODE (x, code);
229 /* Given that MACRO is being expanded as VALUE, apply the appropriate
230 string substitutions to STRING. Return the new string if any changes
231 were needed, otherwise return STRING itself. */
233 static const char *
234 apply_macro_to_string (const char *string, struct mapping *macro, int value)
236 char *base, *copy, *p, *attr, *start, *end;
237 struct mapping *m;
238 struct map_value *v;
240 if (string == 0)
241 return string;
243 base = p = copy = ASTRDUP (string);
244 while ((start = index (p, '<')) && (end = index (start, '>')))
246 p = start + 1;
248 /* If there's a "macro:" prefix, check whether the macro name matches.
249 Set ATTR to the start of the attribute name. */
250 attr = index (p, ':');
251 if (attr == 0 || attr > end)
252 attr = p;
253 else
255 if (strncmp (p, macro->name, attr - p) != 0
256 || macro->name[attr - p] != 0)
257 continue;
258 attr++;
261 /* Find the attribute specification. */
262 *end = 0;
263 m = (struct mapping *) htab_find (macro->group->attrs, &attr);
264 *end = '>';
265 if (m == 0)
266 continue;
268 /* Find the attribute value for VALUE. */
269 for (v = m->values; v != 0; v = v->next)
270 if (v->number == value)
271 break;
272 if (v == 0)
273 continue;
275 /* Add everything between the last copied byte and the '<',
276 then add in the attribute value. */
277 obstack_grow (&string_obstack, base, start - base);
278 obstack_grow (&string_obstack, v->string, strlen (v->string));
279 base = end + 1;
281 if (base != copy)
283 obstack_grow (&string_obstack, base, strlen (base) + 1);
284 return (char *) obstack_finish (&string_obstack);
286 return string;
289 /* Return a copy of ORIGINAL in which all uses of MACRO have been
290 replaced by VALUE. */
292 static rtx
293 apply_macro_to_rtx (rtx original, struct mapping *macro, int value)
295 struct macro_group *group;
296 const char *format_ptr;
297 int i, j;
298 rtx x;
299 enum rtx_code bellwether_code;
301 if (original == 0)
302 return original;
304 /* Create a shallow copy of ORIGINAL. */
305 bellwether_code = BELLWETHER_CODE (GET_CODE (original));
306 x = rtx_alloc (bellwether_code);
307 memcpy (x, original, RTX_SIZE (bellwether_code));
309 /* Change the mode or code itself. */
310 group = macro->group;
311 if (group->uses_macro_p (x, macro->index + group->num_builtins))
312 group->apply_macro (x, value);
314 /* Change each string and recursively change each rtx. */
315 format_ptr = GET_RTX_FORMAT (bellwether_code);
316 for (i = 0; format_ptr[i] != 0; i++)
317 switch (format_ptr[i])
319 case 'S':
320 case 'T':
321 case 's':
322 XSTR (x, i) = apply_macro_to_string (XSTR (x, i), macro, value);
323 break;
325 case 'e':
326 XEXP (x, i) = apply_macro_to_rtx (XEXP (x, i), macro, value);
327 break;
329 case 'V':
330 case 'E':
331 if (XVEC (original, i))
333 XVEC (x, i) = rtvec_alloc (XVECLEN (original, i));
334 for (j = 0; j < XVECLEN (x, i); j++)
335 XVECEXP (x, i, j) = apply_macro_to_rtx (XVECEXP (original, i, j),
336 macro, value);
338 break;
340 default:
341 break;
343 return x;
346 /* Return true if X (or some subexpression of X) uses macro MACRO. */
348 static bool
349 uses_macro_p (rtx x, struct mapping *macro)
351 struct macro_group *group;
352 const char *format_ptr;
353 int i, j;
355 if (x == 0)
356 return false;
358 group = macro->group;
359 if (group->uses_macro_p (x, macro->index + group->num_builtins))
360 return true;
362 format_ptr = GET_RTX_FORMAT (BELLWETHER_CODE (GET_CODE (x)));
363 for (i = 0; format_ptr[i] != 0; i++)
364 switch (format_ptr[i])
366 case 'e':
367 if (uses_macro_p (XEXP (x, i), macro))
368 return true;
369 break;
371 case 'V':
372 case 'E':
373 if (XVEC (x, i))
374 for (j = 0; j < XVECLEN (x, i); j++)
375 if (uses_macro_p (XVECEXP (x, i, j), macro))
376 return true;
377 break;
379 default:
380 break;
382 return false;
385 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
386 has the form "&& ..." (as used in define_insn_and_splits), assume that
387 EXTRA is already satisfied. Empty strings are treated like "true". */
389 static const char *
390 add_condition_to_string (const char *original, const char *extra)
392 char *result;
394 if (original == 0 || original[0] == 0)
395 return extra;
397 if ((original[0] == '&' && original[1] == '&') || extra[0] == 0)
398 return original;
400 asprintf (&result, "(%s) && (%s)", original, extra);
401 return result;
404 /* Like add_condition, but applied to all conditions in rtx X. */
406 static void
407 add_condition_to_rtx (rtx x, const char *extra)
409 switch (GET_CODE (x))
411 case DEFINE_INSN:
412 case DEFINE_EXPAND:
413 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
414 break;
416 case DEFINE_SPLIT:
417 case DEFINE_PEEPHOLE:
418 case DEFINE_PEEPHOLE2:
419 case DEFINE_COND_EXEC:
420 XSTR (x, 1) = add_condition_to_string (XSTR (x, 1), extra);
421 break;
423 case DEFINE_INSN_AND_SPLIT:
424 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
425 XSTR (x, 4) = add_condition_to_string (XSTR (x, 4), extra);
426 break;
428 default:
429 break;
433 /* A htab_traverse callback. Search the EXPR_LIST given by DATA
434 for rtxes that use the macro in *SLOT. Replace each such rtx
435 with a list of expansions. */
437 static int
438 apply_macro_traverse (void **slot, void *data)
440 struct mapping *macro;
441 struct map_value *v;
442 rtx elem, new_elem, original, x;
444 macro = (struct mapping *) *slot;
445 for (elem = (rtx) data; elem != 0; elem = XEXP (elem, 1))
446 if (uses_macro_p (XEXP (elem, 0), macro))
448 original = XEXP (elem, 0);
449 for (v = macro->values; v != 0; v = v->next)
451 x = apply_macro_to_rtx (original, macro, v->number);
452 add_condition_to_rtx (x, v->string);
453 if (v != macro->values)
455 /* Insert a new EXPR_LIST node after ELEM and put the
456 new expansion there. */
457 new_elem = rtx_alloc (EXPR_LIST);
458 XEXP (new_elem, 1) = XEXP (elem, 1);
459 XEXP (elem, 1) = new_elem;
460 elem = new_elem;
462 XEXP (elem, 0) = x;
465 return 1;
468 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
469 of the mapping, GROUP is the group to which it belongs, and INFILE
470 is the file that defined the mapping. */
472 static struct mapping *
473 add_mapping (struct macro_group *group, htab_t table,
474 const char *name, FILE *infile)
476 struct mapping *m;
477 void **slot;
479 m = XNEW (struct mapping);
480 m->name = xstrdup (name);
481 m->group = group;
482 m->index = htab_elements (table);
483 m->values = 0;
485 slot = htab_find_slot (table, m, INSERT);
486 if (*slot != 0)
487 fatal_with_file_and_line (infile, "`%s' already defined", name);
489 *slot = m;
490 return m;
493 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
494 END_PTR points to the current null terminator for the list; return
495 a pointer the new null terminator. */
497 static struct map_value **
498 add_map_value (struct map_value **end_ptr, int number, const char *string)
500 struct map_value *value;
502 value = XNEW (struct map_value);
503 value->next = 0;
504 value->number = number;
505 value->string = string;
507 *end_ptr = value;
508 return &value->next;
511 /* Do one-time initialization of the mode and code attributes. */
513 static void
514 initialize_macros (void)
516 struct mapping *lower, *upper;
517 struct map_value **lower_ptr, **upper_ptr;
518 char *copy, *p;
519 int i;
521 modes.attrs = htab_create (13, def_hash, def_name_eq_p, 0);
522 modes.macros = htab_create (13, def_hash, def_name_eq_p, 0);
523 modes.num_builtins = MAX_MACHINE_MODE;
524 modes.find_builtin = find_mode;
525 modes.uses_macro_p = uses_mode_macro_p;
526 modes.apply_macro = apply_mode_macro;
528 codes.attrs = htab_create (13, def_hash, def_name_eq_p, 0);
529 codes.macros = htab_create (13, def_hash, def_name_eq_p, 0);
530 codes.num_builtins = NUM_RTX_CODE;
531 codes.find_builtin = find_code;
532 codes.uses_macro_p = uses_code_macro_p;
533 codes.apply_macro = apply_code_macro;
535 lower = add_mapping (&modes, modes.attrs, "mode", 0);
536 upper = add_mapping (&modes, modes.attrs, "MODE", 0);
537 lower_ptr = &lower->values;
538 upper_ptr = &upper->values;
539 for (i = 0; i < MAX_MACHINE_MODE; i++)
541 copy = xstrdup (GET_MODE_NAME (i));
542 for (p = copy; *p != 0; p++)
543 *p = TOLOWER (*p);
545 upper_ptr = add_map_value (upper_ptr, i, GET_MODE_NAME (i));
546 lower_ptr = add_map_value (lower_ptr, i, copy);
549 lower = add_mapping (&codes, codes.attrs, "code", 0);
550 upper = add_mapping (&codes, codes.attrs, "CODE", 0);
551 lower_ptr = &lower->values;
552 upper_ptr = &upper->values;
553 for (i = 0; i < NUM_RTX_CODE; i++)
555 copy = xstrdup (GET_RTX_NAME (i));
556 for (p = copy; *p != 0; p++)
557 *p = TOUPPER (*p);
559 lower_ptr = add_map_value (lower_ptr, i, GET_RTX_NAME (i));
560 upper_ptr = add_map_value (upper_ptr, i, copy);
564 /* Read chars from INFILE until a non-whitespace char
565 and return that. Comments, both Lisp style and C style,
566 are treated as whitespace.
567 Tools such as genflags use this function. */
570 read_skip_spaces (FILE *infile)
572 int c;
574 while (1)
576 c = getc (infile);
577 switch (c)
579 case '\n':
580 read_rtx_lineno++;
581 break;
583 case ' ': case '\t': case '\f': case '\r':
584 break;
586 case ';':
588 c = getc (infile);
589 while (c != '\n' && c != EOF);
590 read_rtx_lineno++;
591 break;
593 case '/':
595 int prevc;
596 c = getc (infile);
597 if (c != '*')
598 fatal_expected_char (infile, '*', c);
600 prevc = 0;
601 while ((c = getc (infile)) && c != EOF)
603 if (c == '\n')
604 read_rtx_lineno++;
605 else if (prevc == '*' && c == '/')
606 break;
607 prevc = c;
610 break;
612 default:
613 return c;
618 /* Read an rtx code name into the buffer STR[].
619 It is terminated by any of the punctuation chars of rtx printed syntax. */
621 static void
622 read_name (char *str, FILE *infile)
624 char *p;
625 int c;
627 c = read_skip_spaces (infile);
629 p = str;
630 while (1)
632 if (c == ' ' || c == '\n' || c == '\t' || c == '\f' || c == '\r')
633 break;
634 if (c == ':' || c == ')' || c == ']' || c == '"' || c == '/'
635 || c == '(' || c == '[')
637 ungetc (c, infile);
638 break;
640 *p++ = c;
641 c = getc (infile);
643 if (p == str)
644 fatal_with_file_and_line (infile, "missing name or number");
645 if (c == '\n')
646 read_rtx_lineno++;
648 *p = 0;
650 if (md_constants)
652 /* Do constant expansion. */
653 struct md_constant *def;
655 p = str;
658 struct md_constant tmp_def;
660 tmp_def.name = p;
661 def = (struct md_constant *) htab_find (md_constants, &tmp_def);
662 if (def)
663 p = def->value;
664 } while (def);
665 if (p != str)
666 strcpy (str, p);
670 /* Subroutine of the string readers. Handles backslash escapes.
671 Caller has read the backslash, but not placed it into the obstack. */
672 static void
673 read_escape (FILE *infile)
675 int c = getc (infile);
677 switch (c)
679 /* Backslash-newline is replaced by nothing, as in C. */
680 case '\n':
681 read_rtx_lineno++;
682 return;
684 /* \" \' \\ are replaced by the second character. */
685 case '\\':
686 case '"':
687 case '\'':
688 break;
690 /* Standard C string escapes:
691 \a \b \f \n \r \t \v
692 \[0-7] \x
693 all are passed through to the output string unmolested.
694 In normal use these wind up in a string constant processed
695 by the C compiler, which will translate them appropriately.
696 We do not bother checking that \[0-7] are followed by up to
697 two octal digits, or that \x is followed by N hex digits.
698 \? \u \U are left out because they are not in traditional C. */
699 case 'a': case 'b': case 'f': case 'n': case 'r': case 't': case 'v':
700 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
701 case '7': case 'x':
702 obstack_1grow (&string_obstack, '\\');
703 break;
705 /* \; makes stuff for a C string constant containing
706 newline and tab. */
707 case ';':
708 obstack_grow (&string_obstack, "\\n\\t", 4);
709 return;
711 /* pass anything else through, but issue a warning. */
712 default:
713 fprintf (stderr, "%s:%d: warning: unrecognized escape \\%c\n",
714 read_rtx_filename, read_rtx_lineno, c);
715 obstack_1grow (&string_obstack, '\\');
716 break;
719 obstack_1grow (&string_obstack, c);
723 /* Read a double-quoted string onto the obstack. Caller has scanned
724 the leading quote. */
725 static char *
726 read_quoted_string (FILE *infile)
728 int c;
730 while (1)
732 c = getc (infile); /* Read the string */
733 if (c == '\n')
734 read_rtx_lineno++;
735 else if (c == '\\')
737 read_escape (infile);
738 continue;
740 else if (c == '"')
741 break;
743 obstack_1grow (&string_obstack, c);
746 obstack_1grow (&string_obstack, 0);
747 return (char *) obstack_finish (&string_obstack);
750 /* Read a braced string (a la Tcl) onto the string obstack. Caller
751 has scanned the leading brace. Note that unlike quoted strings,
752 the outermost braces _are_ included in the string constant. */
753 static char *
754 read_braced_string (FILE *infile)
756 int c;
757 int brace_depth = 1; /* caller-processed */
758 unsigned long starting_read_rtx_lineno = read_rtx_lineno;
760 obstack_1grow (&string_obstack, '{');
761 while (brace_depth)
763 c = getc (infile); /* Read the string */
765 if (c == '\n')
766 read_rtx_lineno++;
767 else if (c == '{')
768 brace_depth++;
769 else if (c == '}')
770 brace_depth--;
771 else if (c == '\\')
773 read_escape (infile);
774 continue;
776 else if (c == EOF)
777 fatal_with_file_and_line
778 (infile, "missing closing } for opening brace on line %lu",
779 starting_read_rtx_lineno);
781 obstack_1grow (&string_obstack, c);
784 obstack_1grow (&string_obstack, 0);
785 return (char *) obstack_finish (&string_obstack);
788 /* Read some kind of string constant. This is the high-level routine
789 used by read_rtx. It handles surrounding parentheses, leading star,
790 and dispatch to the appropriate string constant reader. */
792 static char *
793 read_string (FILE *infile, int star_if_braced)
795 char *stringbuf;
796 int saw_paren = 0;
797 int c;
799 c = read_skip_spaces (infile);
800 if (c == '(')
802 saw_paren = 1;
803 c = read_skip_spaces (infile);
806 if (c == '"')
807 stringbuf = read_quoted_string (infile);
808 else if (c == '{')
810 if (star_if_braced)
811 obstack_1grow (&string_obstack, '*');
812 stringbuf = read_braced_string (infile);
814 else
815 fatal_with_file_and_line (infile, "expected `\"' or `{', found `%c'", c);
817 if (saw_paren)
819 c = read_skip_spaces (infile);
820 if (c != ')')
821 fatal_expected_char (infile, ')', c);
824 return stringbuf;
827 /* Provide a version of a function to read a long long if the system does
828 not provide one. */
829 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !defined(HAVE_ATOLL) && !defined(HAVE_ATOQ)
830 HOST_WIDE_INT atoll (const char *);
832 HOST_WIDE_INT
833 atoll (const char *p)
835 int neg = 0;
836 HOST_WIDE_INT tmp_wide;
838 while (ISSPACE (*p))
839 p++;
840 if (*p == '-')
841 neg = 1, p++;
842 else if (*p == '+')
843 p++;
845 tmp_wide = 0;
846 while (ISDIGIT (*p))
848 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
849 if (new_wide < tmp_wide)
851 /* Return INT_MAX equiv on overflow. */
852 tmp_wide = (~(unsigned HOST_WIDE_INT) 0) >> 1;
853 break;
855 tmp_wide = new_wide;
856 p++;
859 if (neg)
860 tmp_wide = -tmp_wide;
861 return tmp_wide;
863 #endif
865 /* Given an object that starts with a char * name field, return a hash
866 code for its name. */
867 static hashval_t
868 def_hash (const void *def)
870 unsigned result, i;
871 const char *string = *(const char *const *) def;
873 for (result = i = 0; *string++ != '\0'; i++)
874 result += ((unsigned char) *string << (i % CHAR_BIT));
875 return result;
878 /* Given two objects that start with char * name fields, return true if
879 they have the same name. */
880 static int
881 def_name_eq_p (const void *def1, const void *def2)
883 return ! strcmp (*(const char *const *) def1,
884 *(const char *const *) def2);
887 /* INFILE is a FILE pointer to read text from. TMP_CHAR is a buffer suitable
888 to read a name or number into. Process a define_constants directive,
889 starting with the optional space after the "define_constants". */
890 static void
891 read_constants (FILE *infile, char *tmp_char)
893 int c;
894 htab_t defs;
896 c = read_skip_spaces (infile);
897 if (c != '[')
898 fatal_expected_char (infile, '[', c);
899 defs = md_constants;
900 if (! defs)
901 defs = htab_create (32, def_hash, def_name_eq_p, (htab_del) 0);
902 /* Disable constant expansion during definition processing. */
903 md_constants = 0;
904 while ( (c = read_skip_spaces (infile)) != ']')
906 struct md_constant *def;
907 void **entry_ptr;
909 if (c != '(')
910 fatal_expected_char (infile, '(', c);
911 def = XNEW (struct md_constant);
912 def->name = tmp_char;
913 read_name (tmp_char, infile);
914 entry_ptr = htab_find_slot (defs, def, INSERT);
915 if (! *entry_ptr)
916 def->name = xstrdup (tmp_char);
917 c = read_skip_spaces (infile);
918 ungetc (c, infile);
919 read_name (tmp_char, infile);
920 if (! *entry_ptr)
922 def->value = xstrdup (tmp_char);
923 *entry_ptr = def;
925 else
927 def = (struct md_constant *) *entry_ptr;
928 if (strcmp (def->value, tmp_char))
929 fatal_with_file_and_line (infile,
930 "redefinition of %s, was %s, now %s",
931 def->name, def->value, tmp_char);
933 c = read_skip_spaces (infile);
934 if (c != ')')
935 fatal_expected_char (infile, ')', c);
937 md_constants = defs;
938 c = read_skip_spaces (infile);
939 if (c != ')')
940 fatal_expected_char (infile, ')', c);
943 /* For every constant definition, call CALLBACK with two arguments:
944 a pointer a pointer to the constant definition and INFO.
945 Stops when CALLBACK returns zero. */
946 void
947 traverse_md_constants (htab_trav callback, void *info)
949 if (md_constants)
950 htab_traverse (md_constants, callback, info);
953 static void
954 validate_const_int (FILE *infile, const char *string)
956 const char *cp;
957 int valid = 1;
959 cp = string;
960 while (*cp && ISSPACE (*cp))
961 cp++;
962 if (*cp == '-' || *cp == '+')
963 cp++;
964 if (*cp == 0)
965 valid = 0;
966 for (; *cp; cp++)
967 if (! ISDIGIT (*cp))
968 valid = 0;
969 if (!valid)
970 fatal_with_file_and_line (infile, "invalid decimal constant \"%s\"\n", string);
973 /* Search GROUP for a mode or code called NAME and return its numerical
974 identifier. INFILE is the file that contained NAME. */
976 static int
977 find_macro (struct macro_group *group, const char *name, FILE *infile)
979 struct mapping *m;
981 m = (struct mapping *) htab_find (group->macros, &name);
982 if (m != 0)
983 return m->index + group->num_builtins;
984 return group->find_builtin (name, infile);
987 /* Finish reading a declaration of the form:
989 (define... <name> [<value1> ... <valuen>])
991 from INFILE, where each <valuei> is either a bare symbol name or a
992 "(<name> <string>)" pair. The "(define..." part has already been read.
994 Represent the declaration as a "mapping" structure; add it to TABLE
995 (which belongs to GROUP) and return it. */
997 static struct mapping *
998 read_mapping (struct macro_group *group, htab_t table, FILE *infile)
1000 char tmp_char[256];
1001 struct mapping *m;
1002 struct map_value **end_ptr;
1003 const char *string;
1004 int number, c;
1006 /* Read the mapping name and create a structure for it. */
1007 read_name (tmp_char, infile);
1008 m = add_mapping (group, table, tmp_char, infile);
1010 c = read_skip_spaces (infile);
1011 if (c != '[')
1012 fatal_expected_char (infile, '[', c);
1014 /* Read each value. */
1015 end_ptr = &m->values;
1016 c = read_skip_spaces (infile);
1019 if (c != '(')
1021 /* A bare symbol name that is implicitly paired to an
1022 empty string. */
1023 ungetc (c, infile);
1024 read_name (tmp_char, infile);
1025 string = "";
1027 else
1029 /* A "(name string)" pair. */
1030 read_name (tmp_char, infile);
1031 string = read_string (infile, false);
1032 c = read_skip_spaces (infile);
1033 if (c != ')')
1034 fatal_expected_char (infile, ')', c);
1036 number = group->find_builtin (tmp_char, infile);
1037 end_ptr = add_map_value (end_ptr, number, string);
1038 c = read_skip_spaces (infile);
1040 while (c != ']');
1042 c = read_skip_spaces (infile);
1043 if (c != ')')
1044 fatal_expected_char (infile, ')', c);
1046 return m;
1049 /* Check newly-created code macro MACRO to see whether every code has the
1050 same format. Initialize the macro's entry in bellwether_codes. */
1052 static void
1053 check_code_macro (struct mapping *macro, FILE *infile)
1055 struct map_value *v;
1056 enum rtx_code bellwether;
1058 bellwether = macro->values->number;
1059 for (v = macro->values->next; v != 0; v = v->next)
1060 if (strcmp (GET_RTX_FORMAT (bellwether), GET_RTX_FORMAT (v->number)) != 0)
1061 fatal_with_file_and_line (infile, "code macro `%s' combines "
1062 "different rtx formats", macro->name);
1064 bellwether_codes = XRESIZEVEC (enum rtx_code, bellwether_codes,
1065 macro->index + 1);
1066 bellwether_codes[macro->index] = bellwether;
1069 /* Read an rtx in printed representation from INFILE and store its
1070 core representation in *X. Also store the line number of the
1071 opening '(' in *LINENO. Return true on success or false if the
1072 end of file has been reached.
1074 read_rtx is not used in the compiler proper, but rather in
1075 the utilities gen*.c that construct C code from machine descriptions. */
1077 bool
1078 read_rtx (FILE *infile, rtx *x, int *lineno)
1080 static rtx queue_head, queue_next;
1081 static int queue_lineno;
1082 int c;
1084 /* Do one-time initialization. */
1085 if (queue_head == 0)
1087 initialize_macros ();
1088 obstack_init (&string_obstack);
1089 queue_head = rtx_alloc (EXPR_LIST);
1092 if (queue_next == 0)
1094 c = read_skip_spaces (infile);
1095 if (c == EOF)
1096 return false;
1097 ungetc (c, infile);
1099 queue_next = queue_head;
1100 queue_lineno = read_rtx_lineno;
1101 XEXP (queue_next, 0) = read_rtx_1 (infile);
1102 XEXP (queue_next, 1) = 0;
1104 htab_traverse (modes.macros, apply_macro_traverse, queue_next);
1105 htab_traverse (codes.macros, apply_macro_traverse, queue_next);
1108 *x = XEXP (queue_next, 0);
1109 *lineno = queue_lineno;
1110 queue_next = XEXP (queue_next, 1);
1112 return true;
1115 /* Subroutine of read_rtx that reads one construct from INFILE but
1116 doesn't apply any macros. */
1118 static rtx
1119 read_rtx_1 (FILE *infile)
1121 int i;
1122 RTX_CODE real_code, bellwether_code;
1123 const char *format_ptr;
1124 /* tmp_char is a buffer used for reading decimal integers
1125 and names of rtx types and machine modes.
1126 Therefore, 256 must be enough. */
1127 char tmp_char[256];
1128 rtx return_rtx;
1129 int c;
1130 int tmp_int;
1131 HOST_WIDE_INT tmp_wide;
1133 /* Linked list structure for making RTXs: */
1134 struct rtx_list
1136 struct rtx_list *next;
1137 rtx value; /* Value of this node. */
1140 again:
1141 c = read_skip_spaces (infile); /* Should be open paren. */
1142 if (c != '(')
1143 fatal_expected_char (infile, '(', c);
1145 read_name (tmp_char, infile);
1146 if (strcmp (tmp_char, "nil") == 0)
1148 /* (nil) stands for an expression that isn't there. */
1149 c = read_skip_spaces (infile);
1150 if (c != ')')
1151 fatal_expected_char (infile, ')', c);
1152 return 0;
1154 if (strcmp (tmp_char, "define_constants") == 0)
1156 read_constants (infile, tmp_char);
1157 goto again;
1159 if (strcmp (tmp_char, "define_mode_attr") == 0)
1161 read_mapping (&modes, modes.attrs, infile);
1162 goto again;
1164 if (strcmp (tmp_char, "define_mode_macro") == 0)
1166 read_mapping (&modes, modes.macros, infile);
1167 goto again;
1169 if (strcmp (tmp_char, "define_code_attr") == 0)
1171 read_mapping (&codes, codes.attrs, infile);
1172 goto again;
1174 if (strcmp (tmp_char, "define_code_macro") == 0)
1176 check_code_macro (read_mapping (&codes, codes.macros, infile), infile);
1177 goto again;
1179 real_code = find_macro (&codes, tmp_char, infile);
1180 bellwether_code = BELLWETHER_CODE (real_code);
1182 /* If we end up with an insn expression then we free this space below. */
1183 return_rtx = rtx_alloc (bellwether_code);
1184 format_ptr = GET_RTX_FORMAT (bellwether_code);
1185 PUT_CODE (return_rtx, real_code);
1187 /* If what follows is `: mode ', read it and
1188 store the mode in the rtx. */
1190 i = read_skip_spaces (infile);
1191 if (i == ':')
1193 read_name (tmp_char, infile);
1194 PUT_MODE (return_rtx, find_macro (&modes, tmp_char, infile));
1196 else
1197 ungetc (i, infile);
1199 for (i = 0; format_ptr[i] != 0; i++)
1200 switch (format_ptr[i])
1202 /* 0 means a field for internal use only.
1203 Don't expect it to be present in the input. */
1204 case '0':
1205 break;
1207 case 'e':
1208 case 'u':
1209 XEXP (return_rtx, i) = read_rtx_1 (infile);
1210 break;
1212 case 'V':
1213 /* 'V' is an optional vector: if a closeparen follows,
1214 just store NULL for this element. */
1215 c = read_skip_spaces (infile);
1216 ungetc (c, infile);
1217 if (c == ')')
1219 XVEC (return_rtx, i) = 0;
1220 break;
1222 /* Now process the vector. */
1224 case 'E':
1226 /* Obstack to store scratch vector in. */
1227 struct obstack vector_stack;
1228 int list_counter = 0;
1229 rtvec return_vec = NULL_RTVEC;
1231 c = read_skip_spaces (infile);
1232 if (c != '[')
1233 fatal_expected_char (infile, '[', c);
1235 /* Add expressions to a list, while keeping a count. */
1236 obstack_init (&vector_stack);
1237 while ((c = read_skip_spaces (infile)) && c != ']')
1239 ungetc (c, infile);
1240 list_counter++;
1241 obstack_ptr_grow (&vector_stack, read_rtx_1 (infile));
1243 if (list_counter > 0)
1245 return_vec = rtvec_alloc (list_counter);
1246 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
1247 list_counter * sizeof (rtx));
1249 XVEC (return_rtx, i) = return_vec;
1250 obstack_free (&vector_stack, NULL);
1251 /* close bracket gotten */
1253 break;
1255 case 'S':
1256 case 'T':
1257 case 's':
1259 char *stringbuf;
1260 int star_if_braced;
1262 c = read_skip_spaces (infile);
1263 ungetc (c, infile);
1264 if (c == ')')
1266 /* 'S' fields are optional and should be NULL if no string
1267 was given. Also allow normal 's' and 'T' strings to be
1268 omitted, treating them in the same way as empty strings. */
1269 XSTR (return_rtx, i) = (format_ptr[i] == 'S' ? NULL : "");
1270 break;
1273 /* The output template slot of a DEFINE_INSN,
1274 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
1275 gets a star inserted as its first character, if it is
1276 written with a brace block instead of a string constant. */
1277 star_if_braced = (format_ptr[i] == 'T');
1279 stringbuf = read_string (infile, star_if_braced);
1281 /* For insn patterns, we want to provide a default name
1282 based on the file and line, like "*foo.md:12", if the
1283 given name is blank. These are only for define_insn and
1284 define_insn_and_split, to aid debugging. */
1285 if (*stringbuf == '\0'
1286 && i == 0
1287 && (GET_CODE (return_rtx) == DEFINE_INSN
1288 || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT))
1290 char line_name[20];
1291 const char *fn = (read_rtx_filename ? read_rtx_filename : "rtx");
1292 const char *slash;
1293 for (slash = fn; *slash; slash ++)
1294 if (*slash == '/' || *slash == '\\' || *slash == ':')
1295 fn = slash + 1;
1296 obstack_1grow (&string_obstack, '*');
1297 obstack_grow (&string_obstack, fn, strlen (fn));
1298 sprintf (line_name, ":%d", read_rtx_lineno);
1299 obstack_grow (&string_obstack, line_name, strlen (line_name)+1);
1300 stringbuf = (char *) obstack_finish (&string_obstack);
1303 if (star_if_braced)
1304 XTMPL (return_rtx, i) = stringbuf;
1305 else
1306 XSTR (return_rtx, i) = stringbuf;
1308 break;
1310 case 'w':
1311 read_name (tmp_char, infile);
1312 validate_const_int (infile, tmp_char);
1313 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1314 tmp_wide = atoi (tmp_char);
1315 #else
1316 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1317 tmp_wide = atol (tmp_char);
1318 #else
1319 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1320 But prefer not to use our hand-rolled function above either. */
1321 #if defined(HAVE_ATOLL) || !defined(HAVE_ATOQ)
1322 tmp_wide = atoll (tmp_char);
1323 #else
1324 tmp_wide = atoq (tmp_char);
1325 #endif
1326 #endif
1327 #endif
1328 XWINT (return_rtx, i) = tmp_wide;
1329 break;
1331 case 'i':
1332 case 'n':
1333 read_name (tmp_char, infile);
1334 validate_const_int (infile, tmp_char);
1335 tmp_int = atoi (tmp_char);
1336 XINT (return_rtx, i) = tmp_int;
1337 break;
1339 default:
1340 fprintf (stderr,
1341 "switch format wrong in rtl.read_rtx(). format was: %c.\n",
1342 format_ptr[i]);
1343 fprintf (stderr, "\tfile position: %ld\n", ftell (infile));
1344 abort ();
1347 c = read_skip_spaces (infile);
1348 if (c != ')')
1349 fatal_expected_char (infile, ')', c);
1351 return return_rtx;