aarch64-simd.md (aarch64_simd_vec_<su>mult_lo_<mode>, [...]): Separate instruction...
[gcc.git] / gcc / read-md.c
1 /* MD reader for GCC.
2 Copyright (C) 1987, 1988, 1991, 1994, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008, 2010, 2011, 2012
4 Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
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.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "bconfig.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "hashtab.h"
26 #include "errors.h"
27 #include "read-md.h"
28
29 /* Associates PTR (which can be a string, etc.) with the file location
30 specified by FILENAME and LINENO. */
31 struct ptr_loc {
32 const void *ptr;
33 const char *filename;
34 int lineno;
35 };
36
37 /* A singly-linked list of filenames. */
38 struct file_name_list {
39 struct file_name_list *next;
40 const char *fname;
41 };
42
43 /* Obstack used for allocating MD strings. */
44 struct obstack string_obstack;
45
46 /* A table of ptr_locs, hashed on the PTR field. */
47 static htab_t ptr_locs;
48
49 /* An obstack for the above. Plain xmalloc is a bit heavyweight for a
50 small structure like ptr_loc. */
51 static struct obstack ptr_loc_obstack;
52
53 /* A hash table of triples (A, B, C), where each of A, B and C is a condition
54 and A is equivalent to "B && C". This is used to keep track of the source
55 of conditions that are made up of separate MD strings (such as the split
56 condition of a define_insn_and_split). */
57 static htab_t joined_conditions;
58
59 /* An obstack for allocating joined_conditions entries. */
60 static struct obstack joined_conditions_obstack;
61
62 /* The file we are reading. */
63 FILE *read_md_file;
64
65 /* The filename of READ_MD_FILE. */
66 const char *read_md_filename;
67
68 /* The current line number in READ_MD_FILE. */
69 int read_md_lineno;
70
71 /* The name of the toplevel file that indirectly included READ_MD_FILE. */
72 const char *in_fname;
73
74 /* The directory part of IN_FNAME. NULL if IN_FNAME is a bare filename. */
75 static char *base_dir;
76
77 /* The first directory to search. */
78 static struct file_name_list *first_dir_md_include;
79
80 /* A pointer to the null terminator of the md include chain. */
81 static struct file_name_list **last_dir_md_include_ptr = &first_dir_md_include;
82
83 /* This callback will be invoked whenever an md include directive is
84 processed. To be used for creation of the dependency file. */
85 void (*include_callback) (const char *);
86
87 /* The current maximum length of directory names in the search path
88 for include files. (Altered as we get more of them.) */
89 static size_t max_include_len;
90
91 /* A table of md_constant structures, hashed by name. Null if no
92 constant expansion should occur. */
93 static htab_t md_constants;
94
95 /* A table of enum_type structures, hashed by name. */
96 static htab_t enum_types;
97
98 static void handle_file (directive_handler_t);
99
100 /* Given an object that starts with a char * name field, return a hash
101 code for its name. */
102
103 hashval_t
104 leading_string_hash (const void *def)
105 {
106 return htab_hash_string (*(const char *const *) def);
107 }
108
109 /* Given two objects that start with char * name fields, return true if
110 they have the same name. */
111
112 int
113 leading_string_eq_p (const void *def1, const void *def2)
114 {
115 return strcmp (*(const char *const *) def1,
116 *(const char *const *) def2) == 0;
117 }
118
119 /* Return a hash value for the pointer pointed to by DEF. */
120
121 static hashval_t
122 leading_ptr_hash (const void *def)
123 {
124 return htab_hash_pointer (*(const void *const *) def);
125 }
126
127 /* Return true if DEF1 and DEF2 are pointers to the same pointer. */
128
129 static int
130 leading_ptr_eq_p (const void *def1, const void *def2)
131 {
132 return *(const void *const *) def1 == *(const void *const *) def2;
133 }
134
135 /* Associate PTR with the file position given by FILENAME and LINENO. */
136
137 static void
138 set_md_ptr_loc (const void *ptr, const char *filename, int lineno)
139 {
140 struct ptr_loc *loc;
141
142 loc = (struct ptr_loc *) obstack_alloc (&ptr_loc_obstack,
143 sizeof (struct ptr_loc));
144 loc->ptr = ptr;
145 loc->filename = filename;
146 loc->lineno = lineno;
147 *htab_find_slot (ptr_locs, loc, INSERT) = loc;
148 }
149
150 /* Return the position associated with pointer PTR. Return null if no
151 position was set. */
152
153 static const struct ptr_loc *
154 get_md_ptr_loc (const void *ptr)
155 {
156 return (const struct ptr_loc *) htab_find (ptr_locs, &ptr);
157 }
158
159 /* Associate NEW_PTR with the same file position as OLD_PTR. */
160
161 void
162 copy_md_ptr_loc (const void *new_ptr, const void *old_ptr)
163 {
164 const struct ptr_loc *loc = get_md_ptr_loc (old_ptr);
165 if (loc != 0)
166 set_md_ptr_loc (new_ptr, loc->filename, loc->lineno);
167 }
168
169 /* If PTR is associated with a known file position, print a #line
170 directive for it to OUTF. */
171
172 void
173 fprint_md_ptr_loc (FILE *outf, const void *ptr)
174 {
175 const struct ptr_loc *loc = get_md_ptr_loc (ptr);
176 if (loc != 0)
177 fprintf (outf, "#line %d \"%s\"\n", loc->lineno, loc->filename);
178 }
179
180 /* Special fprint_md_ptr_loc for writing to STDOUT. */
181 void
182 print_md_ptr_loc (const void *ptr)
183 {
184 fprint_md_ptr_loc (stdout, ptr);
185 }
186
187 /* Return a condition that satisfies both COND1 and COND2. Either string
188 may be null or empty. */
189
190 const char *
191 join_c_conditions (const char *cond1, const char *cond2)
192 {
193 char *result;
194 const void **entry;
195
196 if (cond1 == 0 || cond1[0] == 0)
197 return cond2;
198
199 if (cond2 == 0 || cond2[0] == 0)
200 return cond1;
201
202 if (strcmp (cond1, cond2) == 0)
203 return cond1;
204
205 result = concat ("(", cond1, ") && (", cond2, ")", NULL);
206 obstack_ptr_grow (&joined_conditions_obstack, result);
207 obstack_ptr_grow (&joined_conditions_obstack, cond1);
208 obstack_ptr_grow (&joined_conditions_obstack, cond2);
209 entry = XOBFINISH (&joined_conditions_obstack, const void **);
210 *htab_find_slot (joined_conditions, entry, INSERT) = entry;
211 return result;
212 }
213
214 /* Print condition COND to OUTF, wrapped in brackets. If COND was created
215 by join_c_conditions, recursively invoke this function for the original
216 conditions and join the result with "&&". Otherwise print a #line
217 directive for COND if its original file position is known. */
218
219 void
220 fprint_c_condition (FILE *outf, const char *cond)
221 {
222 const char **halves = (const char **) htab_find (joined_conditions, &cond);
223 if (halves != 0)
224 {
225 fprintf (outf, "(");
226 fprint_c_condition (outf, halves[1]);
227 fprintf (outf, " && ");
228 fprint_c_condition (outf, halves[2]);
229 fprintf (outf, ")");
230 }
231 else
232 {
233 fputc ('\n', outf);
234 fprint_md_ptr_loc (outf, cond);
235 fprintf (outf, "(%s)", cond);
236 }
237 }
238
239 /* Special fprint_c_condition for writing to STDOUT. */
240
241 void
242 print_c_condition (const char *cond)
243 {
244 fprint_c_condition (stdout, cond);
245 }
246
247 /* A vfprintf-like function for reporting an error against line LINENO
248 of the current MD file. */
249
250 static void ATTRIBUTE_PRINTF(2,0)
251 message_with_line_1 (int lineno, const char *msg, va_list ap)
252 {
253 fprintf (stderr, "%s:%d: ", read_md_filename, lineno);
254 vfprintf (stderr, msg, ap);
255 fputc ('\n', stderr);
256 }
257
258 /* A printf-like function for reporting an error against line LINENO
259 in the current MD file. */
260
261 void
262 message_with_line (int lineno, const char *msg, ...)
263 {
264 va_list ap;
265
266 va_start (ap, msg);
267 message_with_line_1 (lineno, msg, ap);
268 va_end (ap);
269 }
270
271 /* Like message_with_line, but treat the condition as an error. */
272
273 void
274 error_with_line (int lineno, const char *msg, ...)
275 {
276 va_list ap;
277
278 va_start (ap, msg);
279 message_with_line_1 (lineno, msg, ap);
280 va_end (ap);
281 have_error = 1;
282 }
283
284 /* A printf-like function for reporting an error against the current
285 position in the MD file. */
286
287 void
288 fatal_with_file_and_line (const char *msg, ...)
289 {
290 char context[64];
291 size_t i;
292 int c;
293 va_list ap;
294
295 va_start (ap, msg);
296
297 fprintf (stderr, "%s:%d: ", read_md_filename, read_md_lineno);
298 vfprintf (stderr, msg, ap);
299 putc ('\n', stderr);
300
301 /* Gather some following context. */
302 for (i = 0; i < sizeof (context)-1; ++i)
303 {
304 c = read_char ();
305 if (c == EOF)
306 break;
307 if (c == '\r' || c == '\n')
308 {
309 unread_char (c);
310 break;
311 }
312 context[i] = c;
313 }
314 context[i] = '\0';
315
316 fprintf (stderr, "%s:%d: following context is `%s'\n",
317 read_md_filename, read_md_lineno, context);
318
319 va_end (ap);
320 exit (1);
321 }
322
323 /* Report that we found character ACTUAL when we expected to find
324 character EXPECTED. */
325
326 void
327 fatal_expected_char (int expected, int actual)
328 {
329 if (actual == EOF)
330 fatal_with_file_and_line ("expected character `%c', found EOF",
331 expected);
332 else
333 fatal_with_file_and_line ("expected character `%c', found `%c'",
334 expected, actual);
335 }
336
337 /* Read chars from the MD file until a non-whitespace char and return that.
338 Comments, both Lisp style and C style, are treated as whitespace. */
339
340 int
341 read_skip_spaces (void)
342 {
343 int c;
344
345 while (1)
346 {
347 c = read_char ();
348 switch (c)
349 {
350 case ' ': case '\t': case '\f': case '\r': case '\n':
351 break;
352
353 case ';':
354 do
355 c = read_char ();
356 while (c != '\n' && c != EOF);
357 break;
358
359 case '/':
360 {
361 int prevc;
362 c = read_char ();
363 if (c != '*')
364 {
365 unread_char (c);
366 fatal_with_file_and_line ("stray '/' in file");
367 }
368
369 prevc = 0;
370 while ((c = read_char ()) && c != EOF)
371 {
372 if (prevc == '*' && c == '/')
373 break;
374 prevc = c;
375 }
376 }
377 break;
378
379 default:
380 return c;
381 }
382 }
383 }
384
385 /* Read an rtx code name into NAME. It is terminated by any of the
386 punctuation chars of rtx printed syntax. */
387
388 void
389 read_name (struct md_name *name)
390 {
391 int c;
392 size_t i;
393
394 c = read_skip_spaces ();
395
396 i = 0;
397 while (1)
398 {
399 if (c == ' ' || c == '\n' || c == '\t' || c == '\f' || c == '\r'
400 || c == EOF)
401 break;
402 if (c == ':' || c == ')' || c == ']' || c == '"' || c == '/'
403 || c == '(' || c == '[')
404 {
405 unread_char (c);
406 break;
407 }
408
409 if (i == sizeof (name->buffer) - 1)
410 fatal_with_file_and_line ("name too long");
411 name->buffer[i++] = c;
412
413 c = read_char ();
414 }
415
416 if (i == 0)
417 fatal_with_file_and_line ("missing name or number");
418
419 name->buffer[i] = 0;
420 name->string = name->buffer;
421
422 if (md_constants)
423 {
424 /* Do constant expansion. */
425 struct md_constant *def;
426
427 do
428 {
429 struct md_constant tmp_def;
430
431 tmp_def.name = name->string;
432 def = (struct md_constant *) htab_find (md_constants, &tmp_def);
433 if (def)
434 name->string = def->value;
435 }
436 while (def);
437 }
438 }
439
440 /* Subroutine of the string readers. Handles backslash escapes.
441 Caller has read the backslash, but not placed it into the obstack. */
442
443 static void
444 read_escape (void)
445 {
446 int c = read_char ();
447
448 switch (c)
449 {
450 /* Backslash-newline is replaced by nothing, as in C. */
451 case '\n':
452 return;
453
454 /* \" \' \\ are replaced by the second character. */
455 case '\\':
456 case '"':
457 case '\'':
458 break;
459
460 /* Standard C string escapes:
461 \a \b \f \n \r \t \v
462 \[0-7] \x
463 all are passed through to the output string unmolested.
464 In normal use these wind up in a string constant processed
465 by the C compiler, which will translate them appropriately.
466 We do not bother checking that \[0-7] are followed by up to
467 two octal digits, or that \x is followed by N hex digits.
468 \? \u \U are left out because they are not in traditional C. */
469 case 'a': case 'b': case 'f': case 'n': case 'r': case 't': case 'v':
470 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
471 case '7': case 'x':
472 obstack_1grow (&string_obstack, '\\');
473 break;
474
475 /* \; makes stuff for a C string constant containing
476 newline and tab. */
477 case ';':
478 obstack_grow (&string_obstack, "\\n\\t", 4);
479 return;
480
481 /* pass anything else through, but issue a warning. */
482 default:
483 fprintf (stderr, "%s:%d: warning: unrecognized escape \\%c\n",
484 read_md_filename, read_md_lineno, c);
485 obstack_1grow (&string_obstack, '\\');
486 break;
487 }
488
489 obstack_1grow (&string_obstack, c);
490 }
491
492 /* Read a double-quoted string onto the obstack. Caller has scanned
493 the leading quote. */
494
495 char *
496 read_quoted_string (void)
497 {
498 int c;
499
500 while (1)
501 {
502 c = read_char (); /* Read the string */
503 if (c == '\\')
504 {
505 read_escape ();
506 continue;
507 }
508 else if (c == '"' || c == EOF)
509 break;
510
511 obstack_1grow (&string_obstack, c);
512 }
513
514 obstack_1grow (&string_obstack, 0);
515 return XOBFINISH (&string_obstack, char *);
516 }
517
518 /* Read a braced string (a la Tcl) onto the string obstack. Caller
519 has scanned the leading brace. Note that unlike quoted strings,
520 the outermost braces _are_ included in the string constant. */
521
522 static char *
523 read_braced_string (void)
524 {
525 int c;
526 int brace_depth = 1; /* caller-processed */
527 unsigned long starting_read_md_lineno = read_md_lineno;
528
529 obstack_1grow (&string_obstack, '{');
530 while (brace_depth)
531 {
532 c = read_char (); /* Read the string */
533
534 if (c == '{')
535 brace_depth++;
536 else if (c == '}')
537 brace_depth--;
538 else if (c == '\\')
539 {
540 read_escape ();
541 continue;
542 }
543 else if (c == EOF)
544 fatal_with_file_and_line
545 ("missing closing } for opening brace on line %lu",
546 starting_read_md_lineno);
547
548 obstack_1grow (&string_obstack, c);
549 }
550
551 obstack_1grow (&string_obstack, 0);
552 return XOBFINISH (&string_obstack, char *);
553 }
554
555 /* Read some kind of string constant. This is the high-level routine
556 used by read_rtx. It handles surrounding parentheses, leading star,
557 and dispatch to the appropriate string constant reader. */
558
559 char *
560 read_string (int star_if_braced)
561 {
562 char *stringbuf;
563 int saw_paren = 0;
564 int c, old_lineno;
565
566 c = read_skip_spaces ();
567 if (c == '(')
568 {
569 saw_paren = 1;
570 c = read_skip_spaces ();
571 }
572
573 old_lineno = read_md_lineno;
574 if (c == '"')
575 stringbuf = read_quoted_string ();
576 else if (c == '{')
577 {
578 if (star_if_braced)
579 obstack_1grow (&string_obstack, '*');
580 stringbuf = read_braced_string ();
581 }
582 else
583 fatal_with_file_and_line ("expected `\"' or `{', found `%c'", c);
584
585 if (saw_paren)
586 {
587 c = read_skip_spaces ();
588 if (c != ')')
589 fatal_expected_char (')', c);
590 }
591
592 set_md_ptr_loc (stringbuf, read_md_filename, old_lineno);
593 return stringbuf;
594 }
595
596 /* Skip the rest of a construct that started at line LINENO and that
597 is currently nested by DEPTH levels of parentheses. */
598
599 void
600 read_skip_construct (int depth, int lineno)
601 {
602 struct md_name name;
603 int c;
604
605 do
606 {
607 c = read_skip_spaces ();
608 if (c == EOF)
609 {
610 error_with_line (lineno, "unterminated construct");
611 exit (1);
612 }
613 switch (c)
614 {
615 case '(':
616 depth++;
617 break;
618
619 case ')':
620 depth--;
621 break;
622
623 case ':':
624 case '[':
625 case ']':
626 case '/':
627 break;
628
629 case '\"':
630 case '{':
631 unread_char (c);
632 read_string (false);
633 break;
634
635 default:
636 unread_char (c);
637 read_name (&name);
638 break;
639 }
640 }
641 while (depth > 0);
642 unread_char (c);
643 }
644
645 /* Given a string, return the number of comma-separated elements in it.
646 Return 0 for the null string. */
647
648 int
649 n_comma_elts (const char *s)
650 {
651 int n;
652
653 if (*s == '\0')
654 return 0;
655
656 for (n = 1; *s; s++)
657 if (*s == ',')
658 n++;
659
660 return n;
661 }
662
663 /* Given a pointer to a (char *), return a pointer to the beginning of the
664 next comma-separated element in the string. Advance the pointer given
665 to the end of that element. Return NULL if at end of string. Caller
666 is responsible for copying the string if necessary. White space between
667 a comma and an element is ignored. */
668
669 const char *
670 scan_comma_elt (const char **pstr)
671 {
672 const char *start;
673 const char *p = *pstr;
674
675 if (*p == ',')
676 p++;
677 while (ISSPACE(*p))
678 p++;
679
680 if (*p == '\0')
681 return NULL;
682
683 start = p;
684
685 while (*p != ',' && *p != '\0')
686 p++;
687
688 *pstr = p;
689 return start;
690 }
691
692 /* Convert STRING to uppercase. */
693
694 void
695 upcase_string (char *string)
696 {
697 int i;
698
699 for (i = 0; string[i]; i++)
700 string[i] = TOUPPER (string[i]);
701 }
702
703 /* Add a NAME = VALUE definition to md_constants-style hash table DEFS,
704 where both NAME and VALUE are malloc()ed strings. PARENT_ENUM is the
705 enum to which NAME belongs, or null if NAME is a stand-alone constant. */
706
707 static struct md_constant *
708 add_constant (htab_t defs, char *name, char *value,
709 struct enum_type *parent_enum)
710 {
711 struct md_constant *def, tmp_def;
712 void **entry_ptr;
713
714 tmp_def.name = name;
715 entry_ptr = htab_find_slot (defs, &tmp_def, INSERT);
716 if (*entry_ptr)
717 {
718 def = (struct md_constant *) *entry_ptr;
719 if (strcmp (def->value, value) != 0)
720 fatal_with_file_and_line ("redefinition of `%s', was `%s', now `%s'",
721 def->name, def->value, value);
722 else if (parent_enum || def->parent_enum)
723 fatal_with_file_and_line ("redefinition of `%s'", def->name);
724 free (name);
725 free (value);
726 }
727 else
728 {
729 def = XNEW (struct md_constant);
730 def->name = name;
731 def->value = value;
732 def->parent_enum = parent_enum;
733 *entry_ptr = def;
734 }
735 return def;
736 }
737
738 /* Process a define_constants directive, starting with the optional space
739 after the "define_constants". */
740
741 static void
742 handle_constants (void)
743 {
744 int c;
745 htab_t defs;
746
747 c = read_skip_spaces ();
748 if (c != '[')
749 fatal_expected_char ('[', c);
750
751 /* Disable constant expansion during definition processing. */
752 defs = md_constants;
753 md_constants = 0;
754 while ( (c = read_skip_spaces ()) != ']')
755 {
756 struct md_name name, value;
757
758 if (c != '(')
759 fatal_expected_char ('(', c);
760
761 read_name (&name);
762 read_name (&value);
763 add_constant (defs, xstrdup (name.string), xstrdup (value.string), 0);
764
765 c = read_skip_spaces ();
766 if (c != ')')
767 fatal_expected_char (')', c);
768 }
769 md_constants = defs;
770 }
771
772 /* For every constant definition, call CALLBACK with two arguments:
773 a pointer a pointer to the constant definition and INFO.
774 Stop when CALLBACK returns zero. */
775
776 void
777 traverse_md_constants (htab_trav callback, void *info)
778 {
779 htab_traverse (md_constants, callback, info);
780 }
781
782 /* Return a malloc()ed decimal string that represents number NUMBER. */
783
784 static char *
785 decimal_string (int number)
786 {
787 /* A safe overestimate. +1 for sign, +1 for null terminator. */
788 char buffer[sizeof (int) * CHAR_BIT + 1 + 1];
789
790 sprintf (buffer, "%d", number);
791 return xstrdup (buffer);
792 }
793
794 /* Process a define_enum or define_c_enum directive, starting with
795 the optional space after the "define_enum". LINENO is the line
796 number on which the directive started and MD_P is true if the
797 directive is a define_enum rather than a define_c_enum. */
798
799 static void
800 handle_enum (int lineno, bool md_p)
801 {
802 char *enum_name, *value_name;
803 struct md_name name;
804 struct enum_type *def;
805 struct enum_value *ev;
806 void **slot;
807 int c;
808
809 enum_name = read_string (false);
810 slot = htab_find_slot (enum_types, &enum_name, INSERT);
811 if (*slot)
812 {
813 def = (struct enum_type *) *slot;
814 if (def->md_p != md_p)
815 error_with_line (lineno, "redefining `%s' as a different type of enum",
816 enum_name);
817 }
818 else
819 {
820 def = XNEW (struct enum_type);
821 def->name = enum_name;
822 def->md_p = md_p;
823 def->values = 0;
824 def->tail_ptr = &def->values;
825 def->num_values = 0;
826 *slot = def;
827 }
828
829 c = read_skip_spaces ();
830 if (c != '[')
831 fatal_expected_char ('[', c);
832
833 while ((c = read_skip_spaces ()) != ']')
834 {
835 if (c == EOF)
836 {
837 error_with_line (lineno, "unterminated construct");
838 exit (1);
839 }
840 unread_char (c);
841 read_name (&name);
842
843 ev = XNEW (struct enum_value);
844 ev->next = 0;
845 if (md_p)
846 {
847 value_name = concat (def->name, "_", name.string, NULL);
848 upcase_string (value_name);
849 ev->name = xstrdup (name.string);
850 }
851 else
852 {
853 value_name = xstrdup (name.string);
854 ev->name = value_name;
855 }
856 ev->def = add_constant (md_constants, value_name,
857 decimal_string (def->num_values), def);
858
859 *def->tail_ptr = ev;
860 def->tail_ptr = &ev->next;
861 def->num_values++;
862 }
863 }
864
865 /* Try to find the definition of the given enum. Return null on failure. */
866
867 struct enum_type *
868 lookup_enum_type (const char *name)
869 {
870 return (struct enum_type *) htab_find (enum_types, &name);
871 }
872
873 /* For every enum definition, call CALLBACK with two arguments:
874 a pointer to the constant definition and INFO. Stop when CALLBACK
875 returns zero. */
876
877 void
878 traverse_enum_types (htab_trav callback, void *info)
879 {
880 htab_traverse (enum_types, callback, info);
881 }
882
883 /* Process an "include" directive, starting with the optional space
884 after the "include". Read in the file and use HANDLE_DIRECTIVE
885 to process each unknown directive. LINENO is the line number on
886 which the "include" occured. */
887
888 static void
889 handle_include (int lineno, directive_handler_t handle_directive)
890 {
891 const char *filename;
892 const char *old_filename;
893 int old_lineno;
894 char *pathname;
895 FILE *input_file, *old_file;
896
897 filename = read_string (false);
898 input_file = NULL;
899
900 /* If the specified file name is absolute, skip the include stack. */
901 if (!IS_ABSOLUTE_PATH (filename))
902 {
903 struct file_name_list *stackp;
904
905 /* Search the directory path, trying to open the file. */
906 for (stackp = first_dir_md_include; stackp; stackp = stackp->next)
907 {
908 static const char sep[2] = { DIR_SEPARATOR, '\0' };
909
910 pathname = concat (stackp->fname, sep, filename, NULL);
911 input_file = fopen (pathname, "r");
912 if (input_file != NULL)
913 break;
914 free (pathname);
915 }
916 }
917
918 /* If we haven't managed to open the file yet, try combining the
919 filename with BASE_DIR. */
920 if (input_file == NULL)
921 {
922 if (base_dir)
923 pathname = concat (base_dir, filename, NULL);
924 else
925 pathname = xstrdup (filename);
926 input_file = fopen (pathname, "r");
927 }
928
929 if (input_file == NULL)
930 {
931 free (pathname);
932 error_with_line (lineno, "include file `%s' not found", filename);
933 return;
934 }
935
936 /* Save the old cursor. Note that the LINENO argument to this
937 function is the beginning of the include statement, while
938 read_md_lineno has already been advanced. */
939 old_file = read_md_file;
940 old_filename = read_md_filename;
941 old_lineno = read_md_lineno;
942
943 if (include_callback)
944 include_callback (pathname);
945
946 read_md_file = input_file;
947 read_md_filename = pathname;
948 handle_file (handle_directive);
949
950 /* Restore the old cursor. */
951 read_md_file = old_file;
952 read_md_filename = old_filename;
953 read_md_lineno = old_lineno;
954
955 /* Do not free the pathname. It is attached to the various rtx
956 queue elements. */
957 }
958
959 /* Process the current file, assuming that read_md_file and
960 read_md_filename are valid. Use HANDLE_DIRECTIVE to handle
961 unknown directives. */
962
963 static void
964 handle_file (directive_handler_t handle_directive)
965 {
966 struct md_name directive;
967 int c, lineno;
968
969 read_md_lineno = 1;
970 while ((c = read_skip_spaces ()) != EOF)
971 {
972 lineno = read_md_lineno;
973 if (c != '(')
974 fatal_expected_char ('(', c);
975
976 read_name (&directive);
977 if (strcmp (directive.string, "define_constants") == 0)
978 handle_constants ();
979 else if (strcmp (directive.string, "define_enum") == 0)
980 handle_enum (lineno, true);
981 else if (strcmp (directive.string, "define_c_enum") == 0)
982 handle_enum (lineno, false);
983 else if (strcmp (directive.string, "include") == 0)
984 handle_include (lineno, handle_directive);
985 else if (handle_directive)
986 handle_directive (lineno, directive.string);
987 else
988 read_skip_construct (1, lineno);
989
990 c = read_skip_spaces ();
991 if (c != ')')
992 fatal_expected_char (')', c);
993 }
994 fclose (read_md_file);
995 }
996
997 /* Like handle_file, but for top-level files. Set up in_fname and
998 base_dir accordingly. */
999
1000 static void
1001 handle_toplevel_file (directive_handler_t handle_directive)
1002 {
1003 const char *base;
1004
1005 in_fname = read_md_filename;
1006 base = lbasename (in_fname);
1007 if (base == in_fname)
1008 base_dir = NULL;
1009 else
1010 base_dir = xstrndup (in_fname, base - in_fname);
1011
1012 handle_file (handle_directive);
1013 }
1014
1015 /* Parse a -I option with argument ARG. */
1016
1017 static void
1018 parse_include (const char *arg)
1019 {
1020 struct file_name_list *dirtmp;
1021
1022 dirtmp = XNEW (struct file_name_list);
1023 dirtmp->next = 0;
1024 dirtmp->fname = arg;
1025 *last_dir_md_include_ptr = dirtmp;
1026 last_dir_md_include_ptr = &dirtmp->next;
1027 if (strlen (dirtmp->fname) > max_include_len)
1028 max_include_len = strlen (dirtmp->fname);
1029 }
1030
1031 /* The main routine for reading .md files. Try to process all the .md
1032 files specified on the command line and return true if no error occured.
1033
1034 ARGC and ARGV are the arguments to main.
1035
1036 PARSE_OPT, if nonnull, is passed all unknown command-line arguments.
1037 It should return true if it recognizes the argument or false if a
1038 generic error should be reported.
1039
1040 If HANDLE_DIRECTIVE is nonnull, the parser calls it for each
1041 unknown directive, otherwise it just skips such directives.
1042 See the comment above the directive_handler_t definition for
1043 details about the callback's interface. */
1044
1045 bool
1046 read_md_files (int argc, char **argv, bool (*parse_opt) (const char *),
1047 directive_handler_t handle_directive)
1048 {
1049 int i;
1050 bool no_more_options;
1051 bool already_read_stdin;
1052 int num_files;
1053
1054 /* Initialize global data. */
1055 obstack_init (&string_obstack);
1056 ptr_locs = htab_create (161, leading_ptr_hash, leading_ptr_eq_p, 0);
1057 obstack_init (&ptr_loc_obstack);
1058 joined_conditions = htab_create (161, leading_ptr_hash, leading_ptr_eq_p, 0);
1059 obstack_init (&joined_conditions_obstack);
1060 md_constants = htab_create (31, leading_string_hash,
1061 leading_string_eq_p, (htab_del) 0);
1062 enum_types = htab_create (31, leading_string_hash,
1063 leading_string_eq_p, (htab_del) 0);
1064
1065 /* Unlock the stdio streams. */
1066 unlock_std_streams ();
1067
1068 /* First we loop over all the options. */
1069 for (i = 1; i < argc; i++)
1070 if (argv[i][0] == '-')
1071 {
1072 /* An argument consisting of exactly one dash is a request to
1073 read stdin. This will be handled in the second loop. */
1074 if (argv[i][1] == '\0')
1075 continue;
1076
1077 /* An argument consisting of just two dashes causes option
1078 parsing to cease. */
1079 if (argv[i][1] == '-' && argv[i][2] == '\0')
1080 break;
1081
1082 if (argv[i][1] == 'I')
1083 {
1084 if (argv[i][2] != '\0')
1085 parse_include (argv[i] + 2);
1086 else if (++i < argc)
1087 parse_include (argv[i]);
1088 else
1089 fatal ("directory name missing after -I option");
1090 continue;
1091 }
1092
1093 /* The program may have provided a callback so it can
1094 accept its own options. */
1095 if (parse_opt && parse_opt (argv[i]))
1096 continue;
1097
1098 fatal ("invalid option `%s'", argv[i]);
1099 }
1100
1101 /* Now loop over all input files. */
1102 num_files = 0;
1103 no_more_options = false;
1104 already_read_stdin = false;
1105 for (i = 1; i < argc; i++)
1106 {
1107 if (argv[i][0] == '-')
1108 {
1109 if (argv[i][1] == '\0')
1110 {
1111 /* Read stdin. */
1112 if (already_read_stdin)
1113 fatal ("cannot read standard input twice");
1114
1115 read_md_file = stdin;
1116 read_md_filename = "<stdin>";
1117 handle_toplevel_file (handle_directive);
1118 already_read_stdin = true;
1119 continue;
1120 }
1121 else if (argv[i][1] == '-' && argv[i][2] == '\0')
1122 {
1123 /* No further arguments are to be treated as options. */
1124 no_more_options = true;
1125 continue;
1126 }
1127 else if (!no_more_options)
1128 continue;
1129 }
1130
1131 /* If we get here we are looking at a non-option argument, i.e.
1132 a file to be processed. */
1133 read_md_filename = argv[i];
1134 read_md_file = fopen (read_md_filename, "r");
1135 if (read_md_file == 0)
1136 {
1137 perror (read_md_filename);
1138 return false;
1139 }
1140 handle_toplevel_file (handle_directive);
1141 num_files++;
1142 }
1143
1144 /* If we get to this point without having seen any files to process,
1145 read the standard input now. */
1146 if (num_files == 0 && !already_read_stdin)
1147 {
1148 read_md_file = stdin;
1149 read_md_filename = "<stdin>";
1150 handle_toplevel_file (handle_directive);
1151 }
1152
1153 return !have_error;
1154 }