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