Add Cortex-A15 tuning to gcc.dg/uninit-pred-8_a.c
[official-gcc.git] / gcc / read-md.c
blobac28944d67150a8080c2950be346f8aa5dcda771
1 /* MD reader for GCC.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file is compiled twice: once for the generator programs
21 once for the compiler. */
22 #ifdef GENERATOR_FILE
23 #include "bconfig.h"
24 #else
25 #include "config.h"
26 #endif
27 #include "system.h"
28 #include "coretypes.h"
29 #ifdef GENERATOR_FILE
30 #include "errors.h"
31 #endif /* #ifdef GENERATOR_FILE */
32 #include "statistics.h"
33 #include "vec.h"
34 #include "read-md.h"
36 #ifndef GENERATOR_FILE
38 /* Minimal reimplementation of errors.c for use by RTL frontend
39 within cc1. */
41 int have_error = 0;
43 #endif /* #ifndef GENERATOR_FILE */
46 /* Associates PTR (which can be a string, etc.) with the file location
47 specified by FILENAME and LINENO. */
48 struct ptr_loc {
49 const void *ptr;
50 const char *filename;
51 int lineno;
54 /* This callback will be invoked whenever an md include directive is
55 processed. To be used for creation of the dependency file. */
56 void (*include_callback) (const char *);
58 /* Global singleton. */
60 md_reader *md_reader_ptr;
62 /* Given an object that starts with a char * name field, return a hash
63 code for its name. */
65 hashval_t
66 leading_string_hash (const void *def)
68 return htab_hash_string (*(const char *const *) def);
71 /* Given two objects that start with char * name fields, return true if
72 they have the same name. */
74 int
75 leading_string_eq_p (const void *def1, const void *def2)
77 return strcmp (*(const char *const *) def1,
78 *(const char *const *) def2) == 0;
81 /* Return a hash value for the pointer pointed to by DEF. */
83 static hashval_t
84 leading_ptr_hash (const void *def)
86 return htab_hash_pointer (*(const void *const *) def);
89 /* Return true if DEF1 and DEF2 are pointers to the same pointer. */
91 static int
92 leading_ptr_eq_p (const void *def1, const void *def2)
94 return *(const void *const *) def1 == *(const void *const *) def2;
97 /* Associate PTR with the file position given by FILENAME and LINENO. */
99 void
100 md_reader::set_md_ptr_loc (const void *ptr, const char *filename, int lineno)
102 struct ptr_loc *loc;
104 loc = (struct ptr_loc *) obstack_alloc (&m_ptr_loc_obstack,
105 sizeof (struct ptr_loc));
106 loc->ptr = ptr;
107 loc->filename = filename;
108 loc->lineno = lineno;
109 *htab_find_slot (m_ptr_locs, loc, INSERT) = loc;
112 /* Return the position associated with pointer PTR. Return null if no
113 position was set. */
115 const struct ptr_loc *
116 md_reader::get_md_ptr_loc (const void *ptr)
118 return (const struct ptr_loc *) htab_find (m_ptr_locs, &ptr);
121 /* Associate NEW_PTR with the same file position as OLD_PTR. */
123 void
124 md_reader::copy_md_ptr_loc (const void *new_ptr, const void *old_ptr)
126 const struct ptr_loc *loc = get_md_ptr_loc (old_ptr);
127 if (loc != 0)
128 set_md_ptr_loc (new_ptr, loc->filename, loc->lineno);
131 /* If PTR is associated with a known file position, print a #line
132 directive for it to OUTF. */
134 void
135 md_reader::fprint_md_ptr_loc (FILE *outf, const void *ptr)
137 const struct ptr_loc *loc = get_md_ptr_loc (ptr);
138 if (loc != 0)
139 fprintf (outf, "#line %d \"%s\"\n", loc->lineno, loc->filename);
142 /* Special fprint_md_ptr_loc for writing to STDOUT. */
143 void
144 md_reader::print_md_ptr_loc (const void *ptr)
146 fprint_md_ptr_loc (stdout, ptr);
149 /* Return a condition that satisfies both COND1 and COND2. Either string
150 may be null or empty. */
152 const char *
153 md_reader::join_c_conditions (const char *cond1, const char *cond2)
155 char *result;
156 const void **entry;
158 if (cond1 == 0 || cond1[0] == 0)
159 return cond2;
161 if (cond2 == 0 || cond2[0] == 0)
162 return cond1;
164 if (strcmp (cond1, cond2) == 0)
165 return cond1;
167 result = concat ("(", cond1, ") && (", cond2, ")", NULL);
168 obstack_ptr_grow (&m_joined_conditions_obstack, result);
169 obstack_ptr_grow (&m_joined_conditions_obstack, cond1);
170 obstack_ptr_grow (&m_joined_conditions_obstack, cond2);
171 entry = XOBFINISH (&m_joined_conditions_obstack, const void **);
172 *htab_find_slot (m_joined_conditions, entry, INSERT) = entry;
173 return result;
176 /* Print condition COND to OUTF, wrapped in brackets. If COND was created
177 by join_c_conditions, recursively invoke this function for the original
178 conditions and join the result with "&&". Otherwise print a #line
179 directive for COND if its original file position is known. */
181 void
182 md_reader::fprint_c_condition (FILE *outf, const char *cond)
184 const char **halves = (const char **) htab_find (m_joined_conditions, &cond);
185 if (halves != 0)
187 fprintf (outf, "(");
188 fprint_c_condition (outf, halves[1]);
189 fprintf (outf, " && ");
190 fprint_c_condition (outf, halves[2]);
191 fprintf (outf, ")");
193 else
195 fputc ('\n', outf);
196 fprint_md_ptr_loc (outf, cond);
197 fprintf (outf, "(%s)", cond);
201 /* Special fprint_c_condition for writing to STDOUT. */
203 void
204 md_reader::print_c_condition (const char *cond)
206 fprint_c_condition (stdout, cond);
209 /* A vfprintf-like function for reporting an error against line LINENO
210 of the current MD file. */
212 static void ATTRIBUTE_PRINTF(2,0)
213 message_at_1 (file_location loc, const char *msg, va_list ap)
215 fprintf (stderr, "%s:%d:%d: ", loc.filename, loc.lineno, loc.colno);
216 vfprintf (stderr, msg, ap);
217 fputc ('\n', stderr);
220 /* A printf-like function for reporting a message against location LOC. */
222 void
223 message_at (file_location loc, const char *msg, ...)
225 va_list ap;
227 va_start (ap, msg);
228 message_at_1 (loc, msg, ap);
229 va_end (ap);
232 /* Like message_at, but treat the condition as an error. */
234 void
235 error_at (file_location loc, const char *msg, ...)
237 va_list ap;
239 va_start (ap, msg);
240 message_at_1 (loc, msg, ap);
241 va_end (ap);
242 have_error = 1;
245 /* Like message_at, but treat the condition as a fatal error. */
247 void
248 fatal_at (file_location loc, const char *msg, ...)
250 va_list ap;
252 va_start (ap, msg);
253 message_at_1 (loc, msg, ap);
254 va_end (ap);
255 exit (1);
258 /* A printf-like function for reporting an error against the current
259 position in the MD file. */
261 void
262 fatal_with_file_and_line (const char *msg, ...)
264 char context[64];
265 size_t i;
266 int c;
267 va_list ap;
269 va_start (ap, msg);
271 fprintf (stderr, "%s:%d:%d: error: ", md_reader_ptr->get_filename (),
272 md_reader_ptr->get_lineno (),
273 md_reader_ptr->get_colno ());
274 vfprintf (stderr, msg, ap);
275 putc ('\n', stderr);
277 /* Gather some following context. */
278 for (i = 0; i < sizeof (context)-1; ++i)
280 c = read_char ();
281 if (c == EOF)
282 break;
283 if (c == '\r' || c == '\n')
285 unread_char (c);
286 break;
288 context[i] = c;
290 context[i] = '\0';
292 fprintf (stderr, "%s:%d:%d: note: following context is `%s'\n",
293 md_reader_ptr->get_filename (),
294 md_reader_ptr->get_lineno (),
295 md_reader_ptr->get_colno (), context);
297 va_end (ap);
298 exit (1);
301 /* Report that we found character ACTUAL when we expected to find
302 character EXPECTED. */
304 void
305 fatal_expected_char (int expected, int actual)
307 if (actual == EOF)
308 fatal_with_file_and_line ("expected character `%c', found EOF",
309 expected);
310 else
311 fatal_with_file_and_line ("expected character `%c', found `%c'",
312 expected, actual);
315 /* Read chars from the MD file until a non-whitespace char and return that.
316 Comments, both Lisp style and C style, are treated as whitespace. */
319 read_skip_spaces (void)
321 int c;
323 while (1)
325 c = read_char ();
326 switch (c)
328 case ' ': case '\t': case '\f': case '\r': case '\n':
329 break;
331 case ';':
333 c = read_char ();
334 while (c != '\n' && c != EOF);
335 break;
337 case '/':
339 int prevc;
340 c = read_char ();
341 if (c != '*')
343 unread_char (c);
344 fatal_with_file_and_line ("stray '/' in file");
347 prevc = 0;
348 while ((c = read_char ()) && c != EOF)
350 if (prevc == '*' && c == '/')
351 break;
352 prevc = c;
355 break;
357 default:
358 return c;
363 /* Consume the next character, issuing a fatal error if it is not
364 EXPECTED. */
366 void
367 md_reader::require_char (char expected)
369 int ch = read_char ();
370 if (ch != expected)
371 fatal_expected_char (expected, ch);
374 /* Consume any whitespace, then consume the next non-whitespace
375 character, issuing a fatal error if it is not EXPECTED. */
377 void
378 md_reader::require_char_ws (char expected)
380 int ch = read_skip_spaces ();
381 if (ch != expected)
382 fatal_expected_char (expected, ch);
385 /* Consume any whitespace, then consume the next word (as per read_name),
386 issuing a fatal error if it is not EXPECTED. */
388 void
389 md_reader::require_word_ws (const char *expected)
391 struct md_name name;
392 read_name (&name);
393 if (strcmp (name.string, expected))
394 fatal_with_file_and_line ("missing '%s'", expected);
397 /* Read the next character from the file. */
400 md_reader::read_char (void)
402 int ch;
404 ch = getc (m_read_md_file);
405 if (ch == '\n')
407 m_read_md_lineno++;
408 m_last_line_colno = m_read_md_colno;
409 m_read_md_colno = 0;
411 else
412 m_read_md_colno++;
414 return ch;
417 /* Put back CH, which was the last character read from the file. */
419 void
420 md_reader::unread_char (int ch)
422 if (ch == '\n')
424 m_read_md_lineno--;
425 m_read_md_colno = m_last_line_colno;
427 else
428 m_read_md_colno--;
429 ungetc (ch, m_read_md_file);
432 /* Peek at the next character from the file without consuming it. */
435 md_reader::peek_char (void)
437 int ch = read_char ();
438 unread_char (ch);
439 return ch;
442 /* Read an rtx code name into NAME. It is terminated by any of the
443 punctuation chars of rtx printed syntax. */
445 bool
446 md_reader::read_name_1 (struct md_name *name, file_location *out_loc)
448 int c;
449 size_t i;
450 int angle_bracket_depth;
452 c = read_skip_spaces ();
454 *out_loc = get_current_location ();
456 i = 0;
457 angle_bracket_depth = 0;
458 while (1)
460 if (c == '<')
461 angle_bracket_depth++;
463 if ((c == '>') && (angle_bracket_depth > 0))
464 angle_bracket_depth--;
466 if (c == ' ' || c == '\n' || c == '\t' || c == '\f' || c == '\r'
467 || c == EOF)
468 break;
469 if (angle_bracket_depth == 0)
471 if (c == ':' || c == ')' || c == ']'
472 || c == '"' || c == '/' || c == '(' || c == '[')
474 unread_char (c);
475 break;
479 if (i == sizeof (name->buffer) - 1)
480 fatal_with_file_and_line ("name too long");
481 name->buffer[i++] = c;
483 c = read_char ();
486 if (i == 0)
487 return false;
489 name->buffer[i] = 0;
490 name->string = name->buffer;
492 if (m_md_constants)
494 /* Do constant expansion. */
495 struct md_constant *def;
499 struct md_constant tmp_def;
501 tmp_def.name = name->string;
502 def = (struct md_constant *) htab_find (m_md_constants, &tmp_def);
503 if (def)
504 name->string = def->value;
506 while (def);
509 return true;
512 /* Read an rtx code name into NAME. It is terminated by any of the
513 punctuation chars of rtx printed syntax. */
515 file_location
516 md_reader::read_name (struct md_name *name)
518 file_location loc;
519 if (!read_name_1 (name, &loc))
520 fatal_with_file_and_line ("missing name or number");
521 return loc;
524 file_location
525 md_reader::read_name_or_nil (struct md_name *name)
527 file_location loc;
528 if (!read_name_1 (name, &loc))
530 file_location loc = get_current_location ();
531 read_skip_construct (0, loc);
532 /* Skip the ')'. */
533 read_char ();
534 name->buffer[0] = 0;
535 name->string = name->buffer;
537 return loc;
540 /* Subroutine of the string readers. Handles backslash escapes.
541 Caller has read the backslash, but not placed it into the obstack. */
543 void
544 md_reader::read_escape ()
546 int c = read_char ();
548 switch (c)
550 /* Backslash-newline is replaced by nothing, as in C. */
551 case '\n':
552 return;
554 /* \" \' \\ are replaced by the second character. */
555 case '\\':
556 case '"':
557 case '\'':
558 break;
560 /* Standard C string escapes:
561 \a \b \f \n \r \t \v
562 \[0-7] \x
563 all are passed through to the output string unmolested.
564 In normal use these wind up in a string constant processed
565 by the C compiler, which will translate them appropriately.
566 We do not bother checking that \[0-7] are followed by up to
567 two octal digits, or that \x is followed by N hex digits.
568 \? \u \U are left out because they are not in traditional C. */
569 case 'a': case 'b': case 'f': case 'n': case 'r': case 't': case 'v':
570 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
571 case '7': case 'x':
572 obstack_1grow (&m_string_obstack, '\\');
573 break;
575 /* \; makes stuff for a C string constant containing
576 newline and tab. */
577 case ';':
578 obstack_grow (&m_string_obstack, "\\n\\t", 4);
579 return;
581 /* pass anything else through, but issue a warning. */
582 default:
583 fprintf (stderr, "%s:%d: warning: unrecognized escape \\%c\n",
584 get_filename (), get_lineno (),
586 obstack_1grow (&m_string_obstack, '\\');
587 break;
590 obstack_1grow (&m_string_obstack, c);
593 /* Read a double-quoted string onto the obstack. Caller has scanned
594 the leading quote. */
596 char *
597 md_reader::read_quoted_string ()
599 int c;
601 while (1)
603 c = read_char (); /* Read the string */
604 if (c == '\\')
606 read_escape ();
607 continue;
609 else if (c == '"' || c == EOF)
610 break;
612 obstack_1grow (&m_string_obstack, c);
615 obstack_1grow (&m_string_obstack, 0);
616 return XOBFINISH (&m_string_obstack, char *);
619 /* Read a braced string (a la Tcl) onto the string obstack. Caller
620 has scanned the leading brace. Note that unlike quoted strings,
621 the outermost braces _are_ included in the string constant. */
623 char *
624 md_reader::read_braced_string ()
626 int c;
627 int brace_depth = 1; /* caller-processed */
628 unsigned long starting_read_md_lineno = get_lineno ();
630 obstack_1grow (&m_string_obstack, '{');
631 while (brace_depth)
633 c = read_char (); /* Read the string */
635 if (c == '{')
636 brace_depth++;
637 else if (c == '}')
638 brace_depth--;
639 else if (c == '\\')
641 read_escape ();
642 continue;
644 else if (c == EOF)
645 fatal_with_file_and_line
646 ("missing closing } for opening brace on line %lu",
647 starting_read_md_lineno);
649 obstack_1grow (&m_string_obstack, c);
652 obstack_1grow (&m_string_obstack, 0);
653 return XOBFINISH (&m_string_obstack, char *);
656 /* Read some kind of string constant. This is the high-level routine
657 used by read_rtx. It handles surrounding parentheses, leading star,
658 and dispatch to the appropriate string constant reader. */
660 char *
661 md_reader::read_string (int star_if_braced)
663 char *stringbuf;
664 int saw_paren = 0;
665 int c, old_lineno;
667 c = read_skip_spaces ();
668 if (c == '(')
670 saw_paren = 1;
671 c = read_skip_spaces ();
674 old_lineno = get_lineno ();
675 if (c == '"')
676 stringbuf = read_quoted_string ();
677 else if (c == '{')
679 if (star_if_braced)
680 obstack_1grow (&m_string_obstack, '*');
681 stringbuf = read_braced_string ();
683 else if (saw_paren && c == 'n')
685 /* Handle (nil) by returning NULL. */
686 require_char ('i');
687 require_char ('l');
688 require_char_ws (')');
689 return NULL;
691 else
692 fatal_with_file_and_line ("expected `\"' or `{', found `%c'", c);
694 if (saw_paren)
695 require_char_ws (')');
697 set_md_ptr_loc (stringbuf, get_filename (), old_lineno);
698 return stringbuf;
701 /* Skip the rest of a construct that started at line LINENO and that
702 is currently nested by DEPTH levels of parentheses. */
704 void
705 md_reader::read_skip_construct (int depth, file_location loc)
707 struct md_name name;
708 int c;
712 c = read_skip_spaces ();
713 if (c == EOF)
715 error_at (loc, "unterminated construct");
716 exit (1);
718 switch (c)
720 case '(':
721 depth++;
722 break;
724 case ')':
725 depth--;
726 break;
728 case ':':
729 case '[':
730 case ']':
731 case '/':
732 break;
734 case '\"':
735 case '{':
736 unread_char (c);
737 read_string (false);
738 break;
740 default:
741 unread_char (c);
742 read_name (&name);
743 break;
746 while (depth > 0);
747 unread_char (c);
750 /* Given a string, return the number of comma-separated elements in it.
751 Return 0 for the null string. */
754 n_comma_elts (const char *s)
756 int n;
758 if (*s == '\0')
759 return 0;
761 for (n = 1; *s; s++)
762 if (*s == ',')
763 n++;
765 return n;
768 /* Given a pointer to a (char *), return a pointer to the beginning of the
769 next comma-separated element in the string. Advance the pointer given
770 to the end of that element. Return NULL if at end of string. Caller
771 is responsible for copying the string if necessary. White space between
772 a comma and an element is ignored. */
774 const char *
775 scan_comma_elt (const char **pstr)
777 const char *start;
778 const char *p = *pstr;
780 if (*p == ',')
781 p++;
782 while (ISSPACE (*p))
783 p++;
785 if (*p == '\0')
786 return NULL;
788 start = p;
790 while (*p != ',' && *p != '\0')
791 p++;
793 *pstr = p;
794 return start;
797 /* Convert STRING to uppercase. */
799 void
800 upcase_string (char *string)
802 int i;
804 for (i = 0; string[i]; i++)
805 string[i] = TOUPPER (string[i]);
808 /* Add a NAME = VALUE definition to md_constants-style hash table DEFS,
809 where both NAME and VALUE are malloc()ed strings. PARENT_ENUM is the
810 enum to which NAME belongs, or null if NAME is a stand-alone constant. */
812 static struct md_constant *
813 add_constant (htab_t defs, char *name, char *value,
814 struct enum_type *parent_enum)
816 struct md_constant *def, tmp_def;
817 void **entry_ptr;
819 tmp_def.name = name;
820 entry_ptr = htab_find_slot (defs, &tmp_def, INSERT);
821 if (*entry_ptr)
823 def = (struct md_constant *) *entry_ptr;
824 if (strcmp (def->value, value) != 0)
825 fatal_with_file_and_line ("redefinition of `%s', was `%s', now `%s'",
826 def->name, def->value, value);
827 else if (parent_enum || def->parent_enum)
828 fatal_with_file_and_line ("redefinition of `%s'", def->name);
829 free (name);
830 free (value);
832 else
834 def = XNEW (struct md_constant);
835 def->name = name;
836 def->value = value;
837 def->parent_enum = parent_enum;
838 *entry_ptr = def;
840 return def;
843 /* Process a define_constants directive, starting with the optional space
844 after the "define_constants". */
846 void
847 md_reader::handle_constants ()
849 int c;
850 htab_t defs;
852 require_char_ws ('[');
854 /* Disable constant expansion during definition processing. */
855 defs = m_md_constants;
856 m_md_constants = 0;
857 while ( (c = read_skip_spaces ()) != ']')
859 struct md_name name, value;
861 if (c != '(')
862 fatal_expected_char ('(', c);
864 read_name (&name);
865 read_name (&value);
866 add_constant (defs, xstrdup (name.string), xstrdup (value.string), 0);
868 require_char_ws (')');
870 m_md_constants = defs;
873 /* For every constant definition, call CALLBACK with two arguments:
874 a pointer a pointer to the constant definition and INFO.
875 Stop when CALLBACK returns zero. */
877 void
878 md_reader::traverse_md_constants (htab_trav callback, void *info)
880 htab_traverse (get_md_constants (), callback, info);
883 /* Return a malloc()ed decimal string that represents number NUMBER. */
885 static char *
886 md_decimal_string (int number)
888 /* A safe overestimate. +1 for sign, +1 for null terminator. */
889 char buffer[sizeof (int) * CHAR_BIT + 1 + 1];
891 sprintf (buffer, "%d", number);
892 return xstrdup (buffer);
895 /* Process a define_enum or define_c_enum directive, starting with
896 the optional space after the "define_enum". LINENO is the line
897 number on which the directive started and MD_P is true if the
898 directive is a define_enum rather than a define_c_enum. */
900 void
901 md_reader::handle_enum (file_location loc, bool md_p)
903 char *enum_name, *value_name;
904 struct md_name name;
905 struct enum_type *def;
906 struct enum_value *ev;
907 void **slot;
908 int c;
910 enum_name = read_string (false);
911 slot = htab_find_slot (m_enum_types, &enum_name, INSERT);
912 if (*slot)
914 def = (struct enum_type *) *slot;
915 if (def->md_p != md_p)
916 error_at (loc, "redefining `%s' as a different type of enum",
917 enum_name);
919 else
921 def = XNEW (struct enum_type);
922 def->name = enum_name;
923 def->md_p = md_p;
924 def->values = 0;
925 def->tail_ptr = &def->values;
926 def->num_values = 0;
927 *slot = def;
930 require_char_ws ('[');
932 while ((c = read_skip_spaces ()) != ']')
934 if (c == EOF)
936 error_at (loc, "unterminated construct");
937 exit (1);
939 unread_char (c);
940 read_name (&name);
942 ev = XNEW (struct enum_value);
943 ev->next = 0;
944 if (md_p)
946 value_name = concat (def->name, "_", name.string, NULL);
947 upcase_string (value_name);
948 ev->name = xstrdup (name.string);
950 else
952 value_name = xstrdup (name.string);
953 ev->name = value_name;
955 ev->def = add_constant (get_md_constants (), value_name,
956 md_decimal_string (def->num_values), def);
958 *def->tail_ptr = ev;
959 def->tail_ptr = &ev->next;
960 def->num_values++;
964 /* Try to find the definition of the given enum. Return null on failure. */
966 struct enum_type *
967 md_reader::lookup_enum_type (const char *name)
969 return (struct enum_type *) htab_find (m_enum_types, &name);
972 /* For every enum definition, call CALLBACK with two arguments:
973 a pointer to the constant definition and INFO. Stop when CALLBACK
974 returns zero. */
976 void
977 md_reader::traverse_enum_types (htab_trav callback, void *info)
979 htab_traverse (m_enum_types, callback, info);
983 /* Constructor for md_reader. */
985 md_reader::md_reader (bool compact)
986 : m_compact (compact),
987 m_toplevel_fname (NULL),
988 m_base_dir (NULL),
989 m_read_md_file (NULL),
990 m_read_md_filename (NULL),
991 m_read_md_lineno (0),
992 m_read_md_colno (0),
993 m_first_dir_md_include (NULL),
994 m_last_dir_md_include_ptr (&m_first_dir_md_include)
996 /* Set the global singleton pointer. */
997 md_reader_ptr = this;
999 obstack_init (&m_string_obstack);
1001 m_ptr_locs = htab_create (161, leading_ptr_hash, leading_ptr_eq_p, 0);
1002 obstack_init (&m_ptr_loc_obstack);
1004 m_joined_conditions = htab_create (161, leading_ptr_hash, leading_ptr_eq_p, 0);
1005 obstack_init (&m_joined_conditions_obstack);
1007 m_md_constants = htab_create (31, leading_string_hash,
1008 leading_string_eq_p, (htab_del) 0);
1010 m_enum_types = htab_create (31, leading_string_hash,
1011 leading_string_eq_p, (htab_del) 0);
1013 /* Unlock the stdio streams. */
1014 unlock_std_streams ();
1017 /* md_reader's destructor. */
1019 md_reader::~md_reader ()
1021 free (m_base_dir);
1023 htab_delete (m_enum_types);
1025 htab_delete (m_md_constants);
1027 obstack_free (&m_joined_conditions_obstack, NULL);
1028 htab_delete (m_joined_conditions);
1030 obstack_free (&m_ptr_loc_obstack, NULL);
1031 htab_delete (m_ptr_locs);
1033 obstack_free (&m_string_obstack, NULL);
1035 /* Clear the global singleton pointer. */
1036 md_reader_ptr = NULL;
1039 /* Process an "include" directive, starting with the optional space
1040 after the "include". Read in the file and use HANDLE_DIRECTIVE
1041 to process each unknown directive. LINENO is the line number on
1042 which the "include" occurred. */
1044 void
1045 md_reader::handle_include (file_location loc)
1047 const char *filename;
1048 const char *old_filename;
1049 int old_lineno, old_colno;
1050 char *pathname;
1051 FILE *input_file, *old_file;
1053 filename = read_string (false);
1054 input_file = NULL;
1056 /* If the specified file name is absolute, skip the include stack. */
1057 if (!IS_ABSOLUTE_PATH (filename))
1059 struct file_name_list *stackp;
1061 /* Search the directory path, trying to open the file. */
1062 for (stackp = m_first_dir_md_include; stackp; stackp = stackp->next)
1064 static const char sep[2] = { DIR_SEPARATOR, '\0' };
1066 pathname = concat (stackp->fname, sep, filename, NULL);
1067 input_file = fopen (pathname, "r");
1068 if (input_file != NULL)
1069 break;
1070 free (pathname);
1074 /* If we haven't managed to open the file yet, try combining the
1075 filename with BASE_DIR. */
1076 if (input_file == NULL)
1078 if (m_base_dir)
1079 pathname = concat (m_base_dir, filename, NULL);
1080 else
1081 pathname = xstrdup (filename);
1082 input_file = fopen (pathname, "r");
1085 if (input_file == NULL)
1087 free (pathname);
1088 error_at (loc, "include file `%s' not found", filename);
1089 return;
1092 /* Save the old cursor. Note that the LINENO argument to this
1093 function is the beginning of the include statement, while
1094 read_md_lineno has already been advanced. */
1095 old_file = m_read_md_file;
1096 old_filename = m_read_md_filename;
1097 old_lineno = m_read_md_lineno;
1098 old_colno = m_read_md_colno;
1100 if (include_callback)
1101 include_callback (pathname);
1103 m_read_md_file = input_file;
1104 m_read_md_filename = pathname;
1106 handle_file ();
1108 /* Restore the old cursor. */
1109 m_read_md_file = old_file;
1110 m_read_md_filename = old_filename;
1111 m_read_md_lineno = old_lineno;
1112 m_read_md_colno = old_colno;
1114 /* Do not free the pathname. It is attached to the various rtx
1115 queue elements. */
1118 /* Process the current file, assuming that read_md_file and
1119 read_md_filename are valid. Use HANDLE_DIRECTIVE to handle
1120 unknown directives. */
1122 void
1123 md_reader::handle_file ()
1125 struct md_name directive;
1126 int c;
1128 m_read_md_lineno = 1;
1129 m_read_md_colno = 0;
1130 while ((c = read_skip_spaces ()) != EOF)
1132 file_location loc = get_current_location ();
1133 if (c != '(')
1134 fatal_expected_char ('(', c);
1136 read_name (&directive);
1137 if (strcmp (directive.string, "define_constants") == 0)
1138 handle_constants ();
1139 else if (strcmp (directive.string, "define_enum") == 0)
1140 handle_enum (loc, true);
1141 else if (strcmp (directive.string, "define_c_enum") == 0)
1142 handle_enum (loc, false);
1143 else if (strcmp (directive.string, "include") == 0)
1144 handle_include (loc);
1145 else
1146 handle_unknown_directive (loc, directive.string);
1148 require_char_ws (')');
1150 fclose (m_read_md_file);
1153 /* Like handle_file, but for top-level files. Set up m_toplevel_fname
1154 and m_base_dir accordingly. */
1156 void
1157 md_reader::handle_toplevel_file ()
1159 const char *base;
1161 m_toplevel_fname = m_read_md_filename;
1162 base = lbasename (m_toplevel_fname);
1163 if (base == m_toplevel_fname)
1164 m_base_dir = NULL;
1165 else
1166 m_base_dir = xstrndup (m_toplevel_fname, base - m_toplevel_fname);
1168 handle_file ();
1171 file_location
1172 md_reader::get_current_location () const
1174 return file_location (m_read_md_filename, m_read_md_lineno, m_read_md_colno);
1177 /* Parse a -I option with argument ARG. */
1179 void
1180 md_reader::add_include_path (const char *arg)
1182 struct file_name_list *dirtmp;
1184 dirtmp = XNEW (struct file_name_list);
1185 dirtmp->next = 0;
1186 dirtmp->fname = arg;
1187 *m_last_dir_md_include_ptr = dirtmp;
1188 m_last_dir_md_include_ptr = &dirtmp->next;
1191 #ifdef GENERATOR_FILE
1193 /* The main routine for reading .md files. Try to process all the .md
1194 files specified on the command line and return true if no error occurred.
1196 ARGC and ARGV are the arguments to main.
1198 PARSE_OPT, if nonnull, is passed all unknown command-line arguments.
1199 It should return true if it recognizes the argument or false if a
1200 generic error should be reported. */
1202 bool
1203 md_reader::read_md_files (int argc, const char **argv,
1204 bool (*parse_opt) (const char *))
1206 int i;
1207 bool no_more_options;
1208 bool already_read_stdin;
1209 int num_files;
1211 /* First we loop over all the options. */
1212 for (i = 1; i < argc; i++)
1213 if (argv[i][0] == '-')
1215 /* An argument consisting of exactly one dash is a request to
1216 read stdin. This will be handled in the second loop. */
1217 if (argv[i][1] == '\0')
1218 continue;
1220 /* An argument consisting of just two dashes causes option
1221 parsing to cease. */
1222 if (argv[i][1] == '-' && argv[i][2] == '\0')
1223 break;
1225 if (argv[i][1] == 'I')
1227 if (argv[i][2] != '\0')
1228 add_include_path (argv[i] + 2);
1229 else if (++i < argc)
1230 add_include_path (argv[i]);
1231 else
1232 fatal ("directory name missing after -I option");
1233 continue;
1236 /* The program may have provided a callback so it can
1237 accept its own options. */
1238 if (parse_opt && parse_opt (argv[i]))
1239 continue;
1241 fatal ("invalid option `%s'", argv[i]);
1244 /* Now loop over all input files. */
1245 num_files = 0;
1246 no_more_options = false;
1247 already_read_stdin = false;
1248 for (i = 1; i < argc; i++)
1250 if (argv[i][0] == '-')
1252 if (argv[i][1] == '\0')
1254 /* Read stdin. */
1255 if (already_read_stdin)
1256 fatal ("cannot read standard input twice");
1258 m_read_md_file = stdin;
1259 m_read_md_filename = "<stdin>";
1260 handle_toplevel_file ();
1261 already_read_stdin = true;
1262 continue;
1264 else if (argv[i][1] == '-' && argv[i][2] == '\0')
1266 /* No further arguments are to be treated as options. */
1267 no_more_options = true;
1268 continue;
1270 else if (!no_more_options)
1271 continue;
1274 /* If we get here we are looking at a non-option argument, i.e.
1275 a file to be processed. */
1276 m_read_md_filename = argv[i];
1277 m_read_md_file = fopen (m_read_md_filename, "r");
1278 if (m_read_md_file == 0)
1280 perror (m_read_md_filename);
1281 return false;
1283 handle_toplevel_file ();
1284 num_files++;
1287 /* If we get to this point without having seen any files to process,
1288 read the standard input now. */
1289 if (num_files == 0 && !already_read_stdin)
1291 m_read_md_file = stdin;
1292 m_read_md_filename = "<stdin>";
1293 handle_toplevel_file ();
1296 return !have_error;
1299 #endif /* #ifdef GENERATOR_FILE */
1301 /* Read FILENAME. */
1303 bool
1304 md_reader::read_file (const char *filename)
1306 m_read_md_filename = filename;
1307 m_read_md_file = fopen (m_read_md_filename, "r");
1308 if (m_read_md_file == 0)
1310 perror (m_read_md_filename);
1311 return false;
1313 handle_toplevel_file ();
1314 return !have_error;
1317 /* class noop_reader : public md_reader */
1319 /* A dummy implementation which skips unknown directives. */
1320 void
1321 noop_reader::handle_unknown_directive (file_location loc, const char *)
1323 read_skip_construct (1, loc);