* doc/install.texi: Fix a typo.
[official-gcc.git] / gcc / read-rtl.c
blobe2ea54a6ed2d33ade5682507cb37972213ecea39
1 /* RTL reader for GCC.
2 Copyright (C) 1987, 1988, 1991, 1994, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005
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 /* Associates PTR (which can be a string, etc.) with the file location
83 specified by FILENAME and LINENO. */
84 struct ptr_loc {
85 const void *ptr;
86 const char *filename;
87 int lineno;
90 /* If CODE is the number of a code macro, return a real rtx code that
91 has the same format. Return CODE otherwise. */
92 #define BELLWETHER_CODE(CODE) \
93 ((CODE) < NUM_RTX_CODE ? CODE : bellwether_codes[CODE - NUM_RTX_CODE])
95 static void fatal_with_file_and_line (FILE *, const char *, ...)
96 ATTRIBUTE_PRINTF_2 ATTRIBUTE_NORETURN;
97 static void fatal_expected_char (FILE *, int, int) ATTRIBUTE_NORETURN;
98 static int find_mode (const char *, FILE *);
99 static bool uses_mode_macro_p (rtx, int);
100 static void apply_mode_macro (rtx, int);
101 static int find_code (const char *, FILE *);
102 static bool uses_code_macro_p (rtx, int);
103 static void apply_code_macro (rtx, int);
104 static const char *apply_macro_to_string (const char *, struct mapping *, int);
105 static rtx apply_macro_to_rtx (rtx, struct mapping *, int);
106 static bool uses_macro_p (rtx, struct mapping *);
107 static const char *add_condition_to_string (const char *, const char *);
108 static void add_condition_to_rtx (rtx, const char *);
109 static int apply_macro_traverse (void **, void *);
110 static struct mapping *add_mapping (struct macro_group *, htab_t t,
111 const char *, FILE *);
112 static struct map_value **add_map_value (struct map_value **,
113 int, const char *);
114 static void initialize_macros (void);
115 static void read_name (char *, FILE *);
116 static hashval_t leading_ptr_hash (const void *);
117 static int leading_ptr_eq_p (const void *, const void *);
118 static void set_rtx_ptr_loc (const void *, const char *, int);
119 static const struct ptr_loc *get_rtx_ptr_loc (const void *);
120 static char *read_string (FILE *, int);
121 static char *read_quoted_string (FILE *);
122 static char *read_braced_string (FILE *);
123 static void read_escape (FILE *);
124 static hashval_t def_hash (const void *);
125 static int def_name_eq_p (const void *, const void *);
126 static void read_constants (FILE *infile, char *tmp_char);
127 static void validate_const_int (FILE *, const char *);
128 static int find_macro (struct macro_group *, const char *, FILE *);
129 static struct mapping *read_mapping (struct macro_group *, htab_t, FILE *);
130 static void check_code_macro (struct mapping *, FILE *);
131 static rtx read_rtx_1 (FILE *);
133 /* The mode and code macro structures. */
134 static struct macro_group modes, codes;
136 /* Index I is the value of BELLWETHER_CODE (I + NUM_RTX_CODE). */
137 static enum rtx_code *bellwether_codes;
139 /* Obstack used for allocating RTL strings. */
140 static struct obstack string_obstack;
142 /* A table of ptr_locs, hashed on the PTR field. */
143 static htab_t ptr_locs;
145 /* An obstack for the above. Plain xmalloc is a bit heavyweight for a
146 small structure like ptr_loc. */
147 static struct obstack ptr_loc_obstack;
149 /* A hash table of triples (A, B, C), where each of A, B and C is a condition
150 and A is equivalent to "B && C". This is used to keep track of the source
151 of conditions that are made up of separate rtx strings (such as the split
152 condition of a define_insn_and_split). */
153 static htab_t joined_conditions;
155 /* An obstack for allocating joined_conditions entries. */
156 static struct obstack joined_conditions_obstack;
158 /* Subroutines of read_rtx. */
160 /* The current line number for the file. */
161 int read_rtx_lineno = 1;
163 /* The filename for aborting with file and line. */
164 const char *read_rtx_filename = "<unknown>";
166 static void
167 fatal_with_file_and_line (FILE *infile, const char *msg, ...)
169 char context[64];
170 size_t i;
171 int c;
172 va_list ap;
174 va_start (ap, msg);
176 fprintf (stderr, "%s:%d: ", read_rtx_filename, read_rtx_lineno);
177 vfprintf (stderr, msg, ap);
178 putc ('\n', stderr);
180 /* Gather some following context. */
181 for (i = 0; i < sizeof (context)-1; ++i)
183 c = getc (infile);
184 if (c == EOF)
185 break;
186 if (c == '\r' || c == '\n')
187 break;
188 context[i] = c;
190 context[i] = '\0';
192 fprintf (stderr, "%s:%d: following context is `%s'\n",
193 read_rtx_filename, read_rtx_lineno, context);
195 va_end (ap);
196 exit (1);
199 /* Dump code after printing a message. Used when read_rtx finds
200 invalid data. */
202 static void
203 fatal_expected_char (FILE *infile, int expected_c, int actual_c)
205 fatal_with_file_and_line (infile, "expected character `%c', found `%c'",
206 expected_c, actual_c);
209 /* Implementations of the macro_group callbacks for modes. */
211 static int
212 find_mode (const char *name, FILE *infile)
214 int i;
216 for (i = 0; i < NUM_MACHINE_MODES; i++)
217 if (strcmp (GET_MODE_NAME (i), name) == 0)
218 return i;
220 fatal_with_file_and_line (infile, "unknown mode `%s'", name);
223 static bool
224 uses_mode_macro_p (rtx x, int mode)
226 return (int) GET_MODE (x) == mode;
229 static void
230 apply_mode_macro (rtx x, int mode)
232 PUT_MODE (x, mode);
235 /* Implementations of the macro_group callbacks for codes. */
237 static int
238 find_code (const char *name, FILE *infile)
240 int i;
242 for (i = 0; i < NUM_RTX_CODE; i++)
243 if (strcmp (GET_RTX_NAME (i), name) == 0)
244 return i;
246 fatal_with_file_and_line (infile, "unknown rtx code `%s'", name);
249 static bool
250 uses_code_macro_p (rtx x, int code)
252 return (int) GET_CODE (x) == code;
255 static void
256 apply_code_macro (rtx x, int code)
258 PUT_CODE (x, code);
261 /* Given that MACRO is being expanded as VALUE, apply the appropriate
262 string substitutions to STRING. Return the new string if any changes
263 were needed, otherwise return STRING itself. */
265 static const char *
266 apply_macro_to_string (const char *string, struct mapping *macro, int value)
268 char *base, *copy, *p, *attr, *start, *end;
269 struct mapping *m;
270 struct map_value *v;
272 if (string == 0)
273 return string;
275 base = p = copy = ASTRDUP (string);
276 while ((start = strchr (p, '<')) && (end = strchr (start, '>')))
278 p = start + 1;
280 /* If there's a "macro:" prefix, check whether the macro name matches.
281 Set ATTR to the start of the attribute name. */
282 attr = strchr (p, ':');
283 if (attr == 0 || attr > end)
284 attr = p;
285 else
287 if (strncmp (p, macro->name, attr - p) != 0
288 || macro->name[attr - p] != 0)
289 continue;
290 attr++;
293 /* Find the attribute specification. */
294 *end = 0;
295 m = (struct mapping *) htab_find (macro->group->attrs, &attr);
296 *end = '>';
297 if (m == 0)
298 continue;
300 /* Find the attribute value for VALUE. */
301 for (v = m->values; v != 0; v = v->next)
302 if (v->number == value)
303 break;
304 if (v == 0)
305 continue;
307 /* Add everything between the last copied byte and the '<',
308 then add in the attribute value. */
309 obstack_grow (&string_obstack, base, start - base);
310 obstack_grow (&string_obstack, v->string, strlen (v->string));
311 base = end + 1;
313 if (base != copy)
315 obstack_grow (&string_obstack, base, strlen (base) + 1);
316 copy = obstack_finish (&string_obstack);
317 copy_rtx_ptr_loc (copy, string);
318 return copy;
320 return string;
323 /* Return a copy of ORIGINAL in which all uses of MACRO have been
324 replaced by VALUE. */
326 static rtx
327 apply_macro_to_rtx (rtx original, struct mapping *macro, int value)
329 struct macro_group *group;
330 const char *format_ptr;
331 int i, j;
332 rtx x;
333 enum rtx_code bellwether_code;
335 if (original == 0)
336 return original;
338 /* Create a shallow copy of ORIGINAL. */
339 bellwether_code = BELLWETHER_CODE (GET_CODE (original));
340 x = rtx_alloc (bellwether_code);
341 memcpy (x, original, RTX_SIZE (bellwether_code));
343 /* Change the mode or code itself. */
344 group = macro->group;
345 if (group->uses_macro_p (x, macro->index + group->num_builtins))
346 group->apply_macro (x, value);
348 /* Change each string and recursively change each rtx. */
349 format_ptr = GET_RTX_FORMAT (bellwether_code);
350 for (i = 0; format_ptr[i] != 0; i++)
351 switch (format_ptr[i])
353 case 'T':
354 XTMPL (x, i) = apply_macro_to_string (XTMPL (x, i), macro, value);
355 break;
357 case 'S':
358 case 's':
359 XSTR (x, i) = apply_macro_to_string (XSTR (x, i), macro, value);
360 break;
362 case 'e':
363 XEXP (x, i) = apply_macro_to_rtx (XEXP (x, i), macro, value);
364 break;
366 case 'V':
367 case 'E':
368 if (XVEC (original, i))
370 XVEC (x, i) = rtvec_alloc (XVECLEN (original, i));
371 for (j = 0; j < XVECLEN (x, i); j++)
372 XVECEXP (x, i, j) = apply_macro_to_rtx (XVECEXP (original, i, j),
373 macro, value);
375 break;
377 default:
378 break;
380 return x;
383 /* Return true if X (or some subexpression of X) uses macro MACRO. */
385 static bool
386 uses_macro_p (rtx x, struct mapping *macro)
388 struct macro_group *group;
389 const char *format_ptr;
390 int i, j;
392 if (x == 0)
393 return false;
395 group = macro->group;
396 if (group->uses_macro_p (x, macro->index + group->num_builtins))
397 return true;
399 format_ptr = GET_RTX_FORMAT (BELLWETHER_CODE (GET_CODE (x)));
400 for (i = 0; format_ptr[i] != 0; i++)
401 switch (format_ptr[i])
403 case 'e':
404 if (uses_macro_p (XEXP (x, i), macro))
405 return true;
406 break;
408 case 'V':
409 case 'E':
410 if (XVEC (x, i))
411 for (j = 0; j < XVECLEN (x, i); j++)
412 if (uses_macro_p (XVECEXP (x, i, j), macro))
413 return true;
414 break;
416 default:
417 break;
419 return false;
422 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
423 has the form "&& ..." (as used in define_insn_and_splits), assume that
424 EXTRA is already satisfied. Empty strings are treated like "true". */
426 static const char *
427 add_condition_to_string (const char *original, const char *extra)
429 if (original != 0 && original[0] == '&' && original[1] == '&')
430 return original;
431 return join_c_conditions (original, extra);
434 /* Like add_condition, but applied to all conditions in rtx X. */
436 static void
437 add_condition_to_rtx (rtx x, const char *extra)
439 switch (GET_CODE (x))
441 case DEFINE_INSN:
442 case DEFINE_EXPAND:
443 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
444 break;
446 case DEFINE_SPLIT:
447 case DEFINE_PEEPHOLE:
448 case DEFINE_PEEPHOLE2:
449 case DEFINE_COND_EXEC:
450 XSTR (x, 1) = add_condition_to_string (XSTR (x, 1), extra);
451 break;
453 case DEFINE_INSN_AND_SPLIT:
454 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
455 XSTR (x, 4) = add_condition_to_string (XSTR (x, 4), extra);
456 break;
458 default:
459 break;
463 /* A htab_traverse callback. Search the EXPR_LIST given by DATA
464 for rtxes that use the macro in *SLOT. Replace each such rtx
465 with a list of expansions. */
467 static int
468 apply_macro_traverse (void **slot, void *data)
470 struct mapping *macro;
471 struct map_value *v;
472 rtx elem, new_elem, original, x;
474 macro = (struct mapping *) *slot;
475 for (elem = (rtx) data; elem != 0; elem = XEXP (elem, 1))
476 if (uses_macro_p (XEXP (elem, 0), macro))
478 original = XEXP (elem, 0);
479 for (v = macro->values; v != 0; v = v->next)
481 x = apply_macro_to_rtx (original, macro, v->number);
482 add_condition_to_rtx (x, v->string);
483 if (v != macro->values)
485 /* Insert a new EXPR_LIST node after ELEM and put the
486 new expansion there. */
487 new_elem = rtx_alloc (EXPR_LIST);
488 XEXP (new_elem, 1) = XEXP (elem, 1);
489 XEXP (elem, 1) = new_elem;
490 elem = new_elem;
492 XEXP (elem, 0) = x;
495 return 1;
498 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
499 of the mapping, GROUP is the group to which it belongs, and INFILE
500 is the file that defined the mapping. */
502 static struct mapping *
503 add_mapping (struct macro_group *group, htab_t table,
504 const char *name, FILE *infile)
506 struct mapping *m;
507 void **slot;
509 m = XNEW (struct mapping);
510 m->name = xstrdup (name);
511 m->group = group;
512 m->index = htab_elements (table);
513 m->values = 0;
515 slot = htab_find_slot (table, m, INSERT);
516 if (*slot != 0)
517 fatal_with_file_and_line (infile, "`%s' already defined", name);
519 *slot = m;
520 return m;
523 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
524 END_PTR points to the current null terminator for the list; return
525 a pointer the new null terminator. */
527 static struct map_value **
528 add_map_value (struct map_value **end_ptr, int number, const char *string)
530 struct map_value *value;
532 value = XNEW (struct map_value);
533 value->next = 0;
534 value->number = number;
535 value->string = string;
537 *end_ptr = value;
538 return &value->next;
541 /* Do one-time initialization of the mode and code attributes. */
543 static void
544 initialize_macros (void)
546 struct mapping *lower, *upper;
547 struct map_value **lower_ptr, **upper_ptr;
548 char *copy, *p;
549 int i;
551 modes.attrs = htab_create (13, def_hash, def_name_eq_p, 0);
552 modes.macros = htab_create (13, def_hash, def_name_eq_p, 0);
553 modes.num_builtins = MAX_MACHINE_MODE;
554 modes.find_builtin = find_mode;
555 modes.uses_macro_p = uses_mode_macro_p;
556 modes.apply_macro = apply_mode_macro;
558 codes.attrs = htab_create (13, def_hash, def_name_eq_p, 0);
559 codes.macros = htab_create (13, def_hash, def_name_eq_p, 0);
560 codes.num_builtins = NUM_RTX_CODE;
561 codes.find_builtin = find_code;
562 codes.uses_macro_p = uses_code_macro_p;
563 codes.apply_macro = apply_code_macro;
565 lower = add_mapping (&modes, modes.attrs, "mode", 0);
566 upper = add_mapping (&modes, modes.attrs, "MODE", 0);
567 lower_ptr = &lower->values;
568 upper_ptr = &upper->values;
569 for (i = 0; i < MAX_MACHINE_MODE; i++)
571 copy = xstrdup (GET_MODE_NAME (i));
572 for (p = copy; *p != 0; p++)
573 *p = TOLOWER (*p);
575 upper_ptr = add_map_value (upper_ptr, i, GET_MODE_NAME (i));
576 lower_ptr = add_map_value (lower_ptr, i, copy);
579 lower = add_mapping (&codes, codes.attrs, "code", 0);
580 upper = add_mapping (&codes, codes.attrs, "CODE", 0);
581 lower_ptr = &lower->values;
582 upper_ptr = &upper->values;
583 for (i = 0; i < NUM_RTX_CODE; i++)
585 copy = xstrdup (GET_RTX_NAME (i));
586 for (p = copy; *p != 0; p++)
587 *p = TOUPPER (*p);
589 lower_ptr = add_map_value (lower_ptr, i, GET_RTX_NAME (i));
590 upper_ptr = add_map_value (upper_ptr, i, copy);
594 /* Return a hash value for the pointer pointed to by DEF. */
596 static hashval_t
597 leading_ptr_hash (const void *def)
599 return htab_hash_pointer (*(const void *const *) def);
602 /* Return true if DEF1 and DEF2 are pointers to the same pointer. */
604 static int
605 leading_ptr_eq_p (const void *def1, const void *def2)
607 return *(const void *const *) def1 == *(const void *const *) def2;
610 /* Associate PTR with the file position given by FILENAME and LINENO. */
612 static void
613 set_rtx_ptr_loc (const void *ptr, const char *filename, int lineno)
615 struct ptr_loc *loc;
617 loc = (struct ptr_loc *) obstack_alloc (&ptr_loc_obstack,
618 sizeof (struct ptr_loc));
619 loc->ptr = ptr;
620 loc->filename = filename;
621 loc->lineno = lineno;
622 *htab_find_slot (ptr_locs, loc, INSERT) = loc;
625 /* Return the position associated with pointer PTR. Return null if no
626 position was set. */
628 static const struct ptr_loc *
629 get_rtx_ptr_loc (const void *ptr)
631 return (const struct ptr_loc *) htab_find (ptr_locs, &ptr);
634 /* Associate NEW_PTR with the same file position as OLD_PTR. */
636 void
637 copy_rtx_ptr_loc (const void *new_ptr, const void *old_ptr)
639 const struct ptr_loc *loc = get_rtx_ptr_loc (old_ptr);
640 if (loc != 0)
641 set_rtx_ptr_loc (new_ptr, loc->filename, loc->lineno);
644 /* If PTR is associated with a known file position, print a #line
645 directive for it. */
647 void
648 print_rtx_ptr_loc (const void *ptr)
650 const struct ptr_loc *loc = get_rtx_ptr_loc (ptr);
651 if (loc != 0)
652 printf ("#line %d \"%s\"\n", loc->lineno, loc->filename);
655 /* Return a condition that satisfies both COND1 and COND2. Either string
656 may be null or empty. */
658 const char *
659 join_c_conditions (const char *cond1, const char *cond2)
661 char *result;
662 const void **entry;
664 if (cond1 == 0 || cond1[0] == 0)
665 return cond2;
667 if (cond2 == 0 || cond2[0] == 0)
668 return cond1;
670 result = concat ("(", cond1, ") && (", cond2, ")", NULL);
671 obstack_ptr_grow (&joined_conditions_obstack, result);
672 obstack_ptr_grow (&joined_conditions_obstack, cond1);
673 obstack_ptr_grow (&joined_conditions_obstack, cond2);
674 entry = obstack_finish (&joined_conditions_obstack);
675 *htab_find_slot (joined_conditions, entry, INSERT) = entry;
676 return result;
679 /* Print condition COND, wrapped in brackets. If COND was created by
680 join_c_conditions, recursively invoke this function for the original
681 conditions and join the result with "&&". Otherwise print a #line
682 directive for COND if its original file position is known. */
684 void
685 print_c_condition (const char *cond)
687 const void **halves = htab_find (joined_conditions, &cond);
688 if (halves != 0)
690 printf ("(");
691 print_c_condition (halves[1]);
692 printf (" && ");
693 print_c_condition (halves[2]);
694 printf (")");
696 else
698 putc ('\n', stdout);
699 print_rtx_ptr_loc (cond);
700 printf ("(%s)", cond);
704 /* Read chars from INFILE until a non-whitespace char
705 and return that. Comments, both Lisp style and C style,
706 are treated as whitespace.
707 Tools such as genflags use this function. */
710 read_skip_spaces (FILE *infile)
712 int c;
714 while (1)
716 c = getc (infile);
717 switch (c)
719 case '\n':
720 read_rtx_lineno++;
721 break;
723 case ' ': case '\t': case '\f': case '\r':
724 break;
726 case ';':
728 c = getc (infile);
729 while (c != '\n' && c != EOF);
730 read_rtx_lineno++;
731 break;
733 case '/':
735 int prevc;
736 c = getc (infile);
737 if (c != '*')
738 fatal_expected_char (infile, '*', c);
740 prevc = 0;
741 while ((c = getc (infile)) && c != EOF)
743 if (c == '\n')
744 read_rtx_lineno++;
745 else if (prevc == '*' && c == '/')
746 break;
747 prevc = c;
750 break;
752 default:
753 return c;
758 /* Read an rtx code name into the buffer STR[].
759 It is terminated by any of the punctuation chars of rtx printed syntax. */
761 static void
762 read_name (char *str, FILE *infile)
764 char *p;
765 int c;
767 c = read_skip_spaces (infile);
769 p = str;
770 while (1)
772 if (c == ' ' || c == '\n' || c == '\t' || c == '\f' || c == '\r')
773 break;
774 if (c == ':' || c == ')' || c == ']' || c == '"' || c == '/'
775 || c == '(' || c == '[')
777 ungetc (c, infile);
778 break;
780 *p++ = c;
781 c = getc (infile);
783 if (p == str)
784 fatal_with_file_and_line (infile, "missing name or number");
785 if (c == '\n')
786 read_rtx_lineno++;
788 *p = 0;
790 if (md_constants)
792 /* Do constant expansion. */
793 struct md_constant *def;
795 p = str;
798 struct md_constant tmp_def;
800 tmp_def.name = p;
801 def = (struct md_constant *) htab_find (md_constants, &tmp_def);
802 if (def)
803 p = def->value;
804 } while (def);
805 if (p != str)
806 strcpy (str, p);
810 /* Subroutine of the string readers. Handles backslash escapes.
811 Caller has read the backslash, but not placed it into the obstack. */
812 static void
813 read_escape (FILE *infile)
815 int c = getc (infile);
817 switch (c)
819 /* Backslash-newline is replaced by nothing, as in C. */
820 case '\n':
821 read_rtx_lineno++;
822 return;
824 /* \" \' \\ are replaced by the second character. */
825 case '\\':
826 case '"':
827 case '\'':
828 break;
830 /* Standard C string escapes:
831 \a \b \f \n \r \t \v
832 \[0-7] \x
833 all are passed through to the output string unmolested.
834 In normal use these wind up in a string constant processed
835 by the C compiler, which will translate them appropriately.
836 We do not bother checking that \[0-7] are followed by up to
837 two octal digits, or that \x is followed by N hex digits.
838 \? \u \U are left out because they are not in traditional C. */
839 case 'a': case 'b': case 'f': case 'n': case 'r': case 't': case 'v':
840 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
841 case '7': case 'x':
842 obstack_1grow (&string_obstack, '\\');
843 break;
845 /* \; makes stuff for a C string constant containing
846 newline and tab. */
847 case ';':
848 obstack_grow (&string_obstack, "\\n\\t", 4);
849 return;
851 /* pass anything else through, but issue a warning. */
852 default:
853 fprintf (stderr, "%s:%d: warning: unrecognized escape \\%c\n",
854 read_rtx_filename, read_rtx_lineno, c);
855 obstack_1grow (&string_obstack, '\\');
856 break;
859 obstack_1grow (&string_obstack, c);
863 /* Read a double-quoted string onto the obstack. Caller has scanned
864 the leading quote. */
865 static char *
866 read_quoted_string (FILE *infile)
868 int c;
870 while (1)
872 c = getc (infile); /* Read the string */
873 if (c == '\n')
874 read_rtx_lineno++;
875 else if (c == '\\')
877 read_escape (infile);
878 continue;
880 else if (c == '"')
881 break;
883 obstack_1grow (&string_obstack, c);
886 obstack_1grow (&string_obstack, 0);
887 return (char *) obstack_finish (&string_obstack);
890 /* Read a braced string (a la Tcl) onto the string obstack. Caller
891 has scanned the leading brace. Note that unlike quoted strings,
892 the outermost braces _are_ included in the string constant. */
893 static char *
894 read_braced_string (FILE *infile)
896 int c;
897 int brace_depth = 1; /* caller-processed */
898 unsigned long starting_read_rtx_lineno = read_rtx_lineno;
900 obstack_1grow (&string_obstack, '{');
901 while (brace_depth)
903 c = getc (infile); /* Read the string */
905 if (c == '\n')
906 read_rtx_lineno++;
907 else if (c == '{')
908 brace_depth++;
909 else if (c == '}')
910 brace_depth--;
911 else if (c == '\\')
913 read_escape (infile);
914 continue;
916 else if (c == EOF)
917 fatal_with_file_and_line
918 (infile, "missing closing } for opening brace on line %lu",
919 starting_read_rtx_lineno);
921 obstack_1grow (&string_obstack, c);
924 obstack_1grow (&string_obstack, 0);
925 return (char *) obstack_finish (&string_obstack);
928 /* Read some kind of string constant. This is the high-level routine
929 used by read_rtx. It handles surrounding parentheses, leading star,
930 and dispatch to the appropriate string constant reader. */
932 static char *
933 read_string (FILE *infile, int star_if_braced)
935 char *stringbuf;
936 int saw_paren = 0;
937 int c, old_lineno;
939 c = read_skip_spaces (infile);
940 if (c == '(')
942 saw_paren = 1;
943 c = read_skip_spaces (infile);
946 old_lineno = read_rtx_lineno;
947 if (c == '"')
948 stringbuf = read_quoted_string (infile);
949 else if (c == '{')
951 if (star_if_braced)
952 obstack_1grow (&string_obstack, '*');
953 stringbuf = read_braced_string (infile);
955 else
956 fatal_with_file_and_line (infile, "expected `\"' or `{', found `%c'", c);
958 if (saw_paren)
960 c = read_skip_spaces (infile);
961 if (c != ')')
962 fatal_expected_char (infile, ')', c);
965 set_rtx_ptr_loc (stringbuf, read_rtx_filename, old_lineno);
966 return stringbuf;
969 /* Provide a version of a function to read a long long if the system does
970 not provide one. */
971 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !defined(HAVE_ATOLL) && !defined(HAVE_ATOQ)
972 HOST_WIDE_INT atoll (const char *);
974 HOST_WIDE_INT
975 atoll (const char *p)
977 int neg = 0;
978 HOST_WIDE_INT tmp_wide;
980 while (ISSPACE (*p))
981 p++;
982 if (*p == '-')
983 neg = 1, p++;
984 else if (*p == '+')
985 p++;
987 tmp_wide = 0;
988 while (ISDIGIT (*p))
990 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
991 if (new_wide < tmp_wide)
993 /* Return INT_MAX equiv on overflow. */
994 tmp_wide = (~(unsigned HOST_WIDE_INT) 0) >> 1;
995 break;
997 tmp_wide = new_wide;
998 p++;
1001 if (neg)
1002 tmp_wide = -tmp_wide;
1003 return tmp_wide;
1005 #endif
1007 /* Given an object that starts with a char * name field, return a hash
1008 code for its name. */
1009 static hashval_t
1010 def_hash (const void *def)
1012 unsigned result, i;
1013 const char *string = *(const char *const *) def;
1015 for (result = i = 0; *string++ != '\0'; i++)
1016 result += ((unsigned char) *string << (i % CHAR_BIT));
1017 return result;
1020 /* Given two objects that start with char * name fields, return true if
1021 they have the same name. */
1022 static int
1023 def_name_eq_p (const void *def1, const void *def2)
1025 return ! strcmp (*(const char *const *) def1,
1026 *(const char *const *) def2);
1029 /* INFILE is a FILE pointer to read text from. TMP_CHAR is a buffer suitable
1030 to read a name or number into. Process a define_constants directive,
1031 starting with the optional space after the "define_constants". */
1032 static void
1033 read_constants (FILE *infile, char *tmp_char)
1035 int c;
1036 htab_t defs;
1038 c = read_skip_spaces (infile);
1039 if (c != '[')
1040 fatal_expected_char (infile, '[', c);
1041 defs = md_constants;
1042 if (! defs)
1043 defs = htab_create (32, def_hash, def_name_eq_p, (htab_del) 0);
1044 /* Disable constant expansion during definition processing. */
1045 md_constants = 0;
1046 while ( (c = read_skip_spaces (infile)) != ']')
1048 struct md_constant *def;
1049 void **entry_ptr;
1051 if (c != '(')
1052 fatal_expected_char (infile, '(', c);
1053 def = XNEW (struct md_constant);
1054 def->name = tmp_char;
1055 read_name (tmp_char, infile);
1056 entry_ptr = htab_find_slot (defs, def, INSERT);
1057 if (! *entry_ptr)
1058 def->name = xstrdup (tmp_char);
1059 c = read_skip_spaces (infile);
1060 ungetc (c, infile);
1061 read_name (tmp_char, infile);
1062 if (! *entry_ptr)
1064 def->value = xstrdup (tmp_char);
1065 *entry_ptr = def;
1067 else
1069 def = (struct md_constant *) *entry_ptr;
1070 if (strcmp (def->value, tmp_char))
1071 fatal_with_file_and_line (infile,
1072 "redefinition of %s, was %s, now %s",
1073 def->name, def->value, tmp_char);
1075 c = read_skip_spaces (infile);
1076 if (c != ')')
1077 fatal_expected_char (infile, ')', c);
1079 md_constants = defs;
1080 c = read_skip_spaces (infile);
1081 if (c != ')')
1082 fatal_expected_char (infile, ')', c);
1085 /* For every constant definition, call CALLBACK with two arguments:
1086 a pointer a pointer to the constant definition and INFO.
1087 Stops when CALLBACK returns zero. */
1088 void
1089 traverse_md_constants (htab_trav callback, void *info)
1091 if (md_constants)
1092 htab_traverse (md_constants, callback, info);
1095 static void
1096 validate_const_int (FILE *infile, const char *string)
1098 const char *cp;
1099 int valid = 1;
1101 cp = string;
1102 while (*cp && ISSPACE (*cp))
1103 cp++;
1104 if (*cp == '-' || *cp == '+')
1105 cp++;
1106 if (*cp == 0)
1107 valid = 0;
1108 for (; *cp; cp++)
1109 if (! ISDIGIT (*cp))
1110 valid = 0;
1111 if (!valid)
1112 fatal_with_file_and_line (infile, "invalid decimal constant \"%s\"\n", string);
1115 /* Search GROUP for a mode or code called NAME and return its numerical
1116 identifier. INFILE is the file that contained NAME. */
1118 static int
1119 find_macro (struct macro_group *group, const char *name, FILE *infile)
1121 struct mapping *m;
1123 m = (struct mapping *) htab_find (group->macros, &name);
1124 if (m != 0)
1125 return m->index + group->num_builtins;
1126 return group->find_builtin (name, infile);
1129 /* Finish reading a declaration of the form:
1131 (define... <name> [<value1> ... <valuen>])
1133 from INFILE, where each <valuei> is either a bare symbol name or a
1134 "(<name> <string>)" pair. The "(define..." part has already been read.
1136 Represent the declaration as a "mapping" structure; add it to TABLE
1137 (which belongs to GROUP) and return it. */
1139 static struct mapping *
1140 read_mapping (struct macro_group *group, htab_t table, FILE *infile)
1142 char tmp_char[256];
1143 struct mapping *m;
1144 struct map_value **end_ptr;
1145 const char *string;
1146 int number, c;
1148 /* Read the mapping name and create a structure for it. */
1149 read_name (tmp_char, infile);
1150 m = add_mapping (group, table, tmp_char, infile);
1152 c = read_skip_spaces (infile);
1153 if (c != '[')
1154 fatal_expected_char (infile, '[', c);
1156 /* Read each value. */
1157 end_ptr = &m->values;
1158 c = read_skip_spaces (infile);
1161 if (c != '(')
1163 /* A bare symbol name that is implicitly paired to an
1164 empty string. */
1165 ungetc (c, infile);
1166 read_name (tmp_char, infile);
1167 string = "";
1169 else
1171 /* A "(name string)" pair. */
1172 read_name (tmp_char, infile);
1173 string = read_string (infile, false);
1174 c = read_skip_spaces (infile);
1175 if (c != ')')
1176 fatal_expected_char (infile, ')', c);
1178 number = group->find_builtin (tmp_char, infile);
1179 end_ptr = add_map_value (end_ptr, number, string);
1180 c = read_skip_spaces (infile);
1182 while (c != ']');
1184 c = read_skip_spaces (infile);
1185 if (c != ')')
1186 fatal_expected_char (infile, ')', c);
1188 return m;
1191 /* Check newly-created code macro MACRO to see whether every code has the
1192 same format. Initialize the macro's entry in bellwether_codes. */
1194 static void
1195 check_code_macro (struct mapping *macro, FILE *infile)
1197 struct map_value *v;
1198 enum rtx_code bellwether;
1200 bellwether = macro->values->number;
1201 for (v = macro->values->next; v != 0; v = v->next)
1202 if (strcmp (GET_RTX_FORMAT (bellwether), GET_RTX_FORMAT (v->number)) != 0)
1203 fatal_with_file_and_line (infile, "code macro `%s' combines "
1204 "different rtx formats", macro->name);
1206 bellwether_codes = XRESIZEVEC (enum rtx_code, bellwether_codes,
1207 macro->index + 1);
1208 bellwether_codes[macro->index] = bellwether;
1211 /* Read an rtx in printed representation from INFILE and store its
1212 core representation in *X. Also store the line number of the
1213 opening '(' in *LINENO. Return true on success or false if the
1214 end of file has been reached.
1216 read_rtx is not used in the compiler proper, but rather in
1217 the utilities gen*.c that construct C code from machine descriptions. */
1219 bool
1220 read_rtx (FILE *infile, rtx *x, int *lineno)
1222 static rtx queue_head, queue_next;
1223 static int queue_lineno;
1224 int c;
1226 /* Do one-time initialization. */
1227 if (queue_head == 0)
1229 initialize_macros ();
1230 obstack_init (&string_obstack);
1231 queue_head = rtx_alloc (EXPR_LIST);
1232 ptr_locs = htab_create (161, leading_ptr_hash, leading_ptr_eq_p, 0);
1233 obstack_init (&ptr_loc_obstack);
1234 joined_conditions = htab_create (161, leading_ptr_hash,
1235 leading_ptr_eq_p, 0);
1236 obstack_init (&joined_conditions_obstack);
1239 if (queue_next == 0)
1241 c = read_skip_spaces (infile);
1242 if (c == EOF)
1243 return false;
1244 ungetc (c, infile);
1246 queue_next = queue_head;
1247 queue_lineno = read_rtx_lineno;
1248 XEXP (queue_next, 0) = read_rtx_1 (infile);
1249 XEXP (queue_next, 1) = 0;
1251 htab_traverse (modes.macros, apply_macro_traverse, queue_next);
1252 htab_traverse (codes.macros, apply_macro_traverse, queue_next);
1255 *x = XEXP (queue_next, 0);
1256 *lineno = queue_lineno;
1257 queue_next = XEXP (queue_next, 1);
1259 return true;
1262 /* Subroutine of read_rtx that reads one construct from INFILE but
1263 doesn't apply any macros. */
1265 static rtx
1266 read_rtx_1 (FILE *infile)
1268 int i;
1269 RTX_CODE real_code, bellwether_code;
1270 const char *format_ptr;
1271 /* tmp_char is a buffer used for reading decimal integers
1272 and names of rtx types and machine modes.
1273 Therefore, 256 must be enough. */
1274 char tmp_char[256];
1275 rtx return_rtx;
1276 int c;
1277 int tmp_int;
1278 HOST_WIDE_INT tmp_wide;
1280 /* Linked list structure for making RTXs: */
1281 struct rtx_list
1283 struct rtx_list *next;
1284 rtx value; /* Value of this node. */
1287 again:
1288 c = read_skip_spaces (infile); /* Should be open paren. */
1289 if (c != '(')
1290 fatal_expected_char (infile, '(', c);
1292 read_name (tmp_char, infile);
1293 if (strcmp (tmp_char, "nil") == 0)
1295 /* (nil) stands for an expression that isn't there. */
1296 c = read_skip_spaces (infile);
1297 if (c != ')')
1298 fatal_expected_char (infile, ')', c);
1299 return 0;
1301 if (strcmp (tmp_char, "define_constants") == 0)
1303 read_constants (infile, tmp_char);
1304 goto again;
1306 if (strcmp (tmp_char, "define_mode_attr") == 0)
1308 read_mapping (&modes, modes.attrs, infile);
1309 goto again;
1311 if (strcmp (tmp_char, "define_mode_macro") == 0)
1313 read_mapping (&modes, modes.macros, infile);
1314 goto again;
1316 if (strcmp (tmp_char, "define_code_attr") == 0)
1318 read_mapping (&codes, codes.attrs, infile);
1319 goto again;
1321 if (strcmp (tmp_char, "define_code_macro") == 0)
1323 check_code_macro (read_mapping (&codes, codes.macros, infile), infile);
1324 goto again;
1326 real_code = find_macro (&codes, tmp_char, infile);
1327 bellwether_code = BELLWETHER_CODE (real_code);
1329 /* If we end up with an insn expression then we free this space below. */
1330 return_rtx = rtx_alloc (bellwether_code);
1331 format_ptr = GET_RTX_FORMAT (bellwether_code);
1332 PUT_CODE (return_rtx, real_code);
1334 /* If what follows is `: mode ', read it and
1335 store the mode in the rtx. */
1337 i = read_skip_spaces (infile);
1338 if (i == ':')
1340 read_name (tmp_char, infile);
1341 PUT_MODE (return_rtx, find_macro (&modes, tmp_char, infile));
1343 else
1344 ungetc (i, infile);
1346 for (i = 0; format_ptr[i] != 0; i++)
1347 switch (format_ptr[i])
1349 /* 0 means a field for internal use only.
1350 Don't expect it to be present in the input. */
1351 case '0':
1352 break;
1354 case 'e':
1355 case 'u':
1356 XEXP (return_rtx, i) = read_rtx_1 (infile);
1357 break;
1359 case 'V':
1360 /* 'V' is an optional vector: if a closeparen follows,
1361 just store NULL for this element. */
1362 c = read_skip_spaces (infile);
1363 ungetc (c, infile);
1364 if (c == ')')
1366 XVEC (return_rtx, i) = 0;
1367 break;
1369 /* Now process the vector. */
1371 case 'E':
1373 /* Obstack to store scratch vector in. */
1374 struct obstack vector_stack;
1375 int list_counter = 0;
1376 rtvec return_vec = NULL_RTVEC;
1378 c = read_skip_spaces (infile);
1379 if (c != '[')
1380 fatal_expected_char (infile, '[', c);
1382 /* Add expressions to a list, while keeping a count. */
1383 obstack_init (&vector_stack);
1384 while ((c = read_skip_spaces (infile)) && c != ']')
1386 ungetc (c, infile);
1387 list_counter++;
1388 obstack_ptr_grow (&vector_stack, read_rtx_1 (infile));
1390 if (list_counter > 0)
1392 return_vec = rtvec_alloc (list_counter);
1393 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
1394 list_counter * sizeof (rtx));
1396 else if (format_ptr[i] == 'E')
1397 fatal_with_file_and_line (infile,
1398 "vector must have at least one element");
1399 XVEC (return_rtx, i) = return_vec;
1400 obstack_free (&vector_stack, NULL);
1401 /* close bracket gotten */
1403 break;
1405 case 'S':
1406 case 'T':
1407 case 's':
1409 char *stringbuf;
1410 int star_if_braced;
1412 c = read_skip_spaces (infile);
1413 ungetc (c, infile);
1414 if (c == ')')
1416 /* 'S' fields are optional and should be NULL if no string
1417 was given. Also allow normal 's' and 'T' strings to be
1418 omitted, treating them in the same way as empty strings. */
1419 XSTR (return_rtx, i) = (format_ptr[i] == 'S' ? NULL : "");
1420 break;
1423 /* The output template slot of a DEFINE_INSN,
1424 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
1425 gets a star inserted as its first character, if it is
1426 written with a brace block instead of a string constant. */
1427 star_if_braced = (format_ptr[i] == 'T');
1429 stringbuf = read_string (infile, star_if_braced);
1431 /* For insn patterns, we want to provide a default name
1432 based on the file and line, like "*foo.md:12", if the
1433 given name is blank. These are only for define_insn and
1434 define_insn_and_split, to aid debugging. */
1435 if (*stringbuf == '\0'
1436 && i == 0
1437 && (GET_CODE (return_rtx) == DEFINE_INSN
1438 || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT))
1440 char line_name[20];
1441 const char *fn = (read_rtx_filename ? read_rtx_filename : "rtx");
1442 const char *slash;
1443 for (slash = fn; *slash; slash ++)
1444 if (*slash == '/' || *slash == '\\' || *slash == ':')
1445 fn = slash + 1;
1446 obstack_1grow (&string_obstack, '*');
1447 obstack_grow (&string_obstack, fn, strlen (fn));
1448 sprintf (line_name, ":%d", read_rtx_lineno);
1449 obstack_grow (&string_obstack, line_name, strlen (line_name)+1);
1450 stringbuf = (char *) obstack_finish (&string_obstack);
1453 if (star_if_braced)
1454 XTMPL (return_rtx, i) = stringbuf;
1455 else
1456 XSTR (return_rtx, i) = stringbuf;
1458 break;
1460 case 'w':
1461 read_name (tmp_char, infile);
1462 validate_const_int (infile, tmp_char);
1463 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1464 tmp_wide = atoi (tmp_char);
1465 #else
1466 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1467 tmp_wide = atol (tmp_char);
1468 #else
1469 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1470 But prefer not to use our hand-rolled function above either. */
1471 #if defined(HAVE_ATOLL) || !defined(HAVE_ATOQ)
1472 tmp_wide = atoll (tmp_char);
1473 #else
1474 tmp_wide = atoq (tmp_char);
1475 #endif
1476 #endif
1477 #endif
1478 XWINT (return_rtx, i) = tmp_wide;
1479 break;
1481 case 'i':
1482 case 'n':
1483 read_name (tmp_char, infile);
1484 validate_const_int (infile, tmp_char);
1485 tmp_int = atoi (tmp_char);
1486 XINT (return_rtx, i) = tmp_int;
1487 break;
1489 default:
1490 fprintf (stderr,
1491 "switch format wrong in rtl.read_rtx(). format was: %c.\n",
1492 format_ptr[i]);
1493 fprintf (stderr, "\tfile position: %ld\n", ftell (infile));
1494 abort ();
1497 c = read_skip_spaces (infile);
1498 if (c != ')')
1499 fatal_expected_char (infile, ')', c);
1501 return return_rtx;