OSDN Git Service

* config/xtensa/xtensa.c (xtensa_init_machine_status): Fix
[pf3gnuchains/gcc-fork.git] / gcc / read-rtl.c
1 /* RTL reader for GNU C Compiler.
2    Copyright (C) 1987, 1988, 1991, 1994, 1997, 1998, 1999, 2000, 2001, 2002
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22 #include "hconfig.h"
23 #include "system.h"
24 #include "rtl.h"
25 #include "obstack.h"
26 #include "hashtab.h"
27
28 #ifndef ISDIGIT
29 #include <ctype.h>
30 #define ISDIGIT isdigit
31 #define ISSPACE isspace
32 #endif
33
34 #define obstack_chunk_alloc     xmalloc
35 #define obstack_chunk_free      free
36
37 static htab_t md_constants;
38
39 static void fatal_with_file_and_line PARAMS ((FILE *, const char *, ...))
40   ATTRIBUTE_PRINTF_2 ATTRIBUTE_NORETURN;
41 static void fatal_expected_char PARAMS ((FILE *, int, int)) ATTRIBUTE_NORETURN;
42 static void read_name           PARAMS ((char *, FILE *));
43 static char *read_string        PARAMS ((struct obstack *, FILE *, int));
44 static char *read_quoted_string PARAMS ((struct obstack *, FILE *));
45 static char *read_braced_string PARAMS ((struct obstack *, FILE *));
46 static void read_escape         PARAMS ((struct obstack *, FILE *));
47 static unsigned def_hash PARAMS ((const void *));
48 static int def_name_eq_p PARAMS ((const void *, const void *));
49 static void read_constants PARAMS ((FILE *infile, char *tmp_char));
50 static void validate_const_int PARAMS ((FILE *, const char *));
51
52 /* Subroutines of read_rtx.  */
53
54 /* The current line number for the file.  */
55 int read_rtx_lineno = 1;
56
57 /* The filename for aborting with file and line.  */
58 const char *read_rtx_filename = "<unknown>";
59
60 static void
61 fatal_with_file_and_line VPARAMS ((FILE *infile, const char *msg, ...))
62 {
63   char context[64];
64   size_t i;
65   int c;
66
67   VA_OPEN (ap, msg);
68   VA_FIXEDARG (ap, FILE *, infile);
69   VA_FIXEDARG (ap, const char *, msg);
70
71   fprintf (stderr, "%s:%d: ", read_rtx_filename, read_rtx_lineno);
72   vfprintf (stderr, msg, ap);
73   putc ('\n', stderr);
74
75   /* Gather some following context.  */
76   for (i = 0; i < sizeof (context)-1; ++i)
77     {
78       c = getc (infile);
79       if (c == EOF)
80         break;
81       if (c == '\r' || c == '\n')
82         break;
83       context[i] = c;
84     }
85   context[i] = '\0';
86
87   fprintf (stderr, "%s:%d: following context is `%s'\n",
88            read_rtx_filename, read_rtx_lineno, context);
89
90   VA_CLOSE (ap);
91   exit (1);
92 }
93
94 /* Dump code after printing a message.  Used when read_rtx finds
95    invalid data.  */
96
97 static void
98 fatal_expected_char (infile, expected_c, actual_c)
99      FILE *infile;
100      int expected_c, actual_c;
101 {
102   fatal_with_file_and_line (infile, "expected character `%c', found `%c'",
103                             expected_c, actual_c);
104 }
105
106 /* Read chars from INFILE until a non-whitespace char
107    and return that.  Comments, both Lisp style and C style,
108    are treated as whitespace.
109    Tools such as genflags use this function.  */
110
111 int
112 read_skip_spaces (infile)
113      FILE *infile;
114 {
115   int c;
116
117   while (1)
118     {
119       c = getc (infile);
120       switch (c)
121         {
122         case '\n':
123           read_rtx_lineno++;
124           break;
125
126         case ' ': case '\t': case '\f': case '\r':
127           break;
128
129         case ';':
130           do
131             c = getc (infile);
132           while (c != '\n' && c != EOF);
133           read_rtx_lineno++;
134           break;
135
136         case '/':
137           {
138             int prevc;
139             c = getc (infile);
140             if (c != '*')
141               fatal_expected_char (infile, '*', c);
142
143             prevc = 0;
144             while ((c = getc (infile)) && c != EOF)
145               {
146                 if (c == '\n')
147                    read_rtx_lineno++;
148                 else if (prevc == '*' && c == '/')
149                   break;
150                 prevc = c;
151               }
152           }
153           break;
154
155         default:
156           return c;
157         }
158     }
159 }
160
161 /* Read an rtx code name into the buffer STR[].
162    It is terminated by any of the punctuation chars of rtx printed syntax.  */
163
164 static void
165 read_name (str, infile)
166      char *str;
167      FILE *infile;
168 {
169   char *p;
170   int c;
171
172   c = read_skip_spaces (infile);
173
174   p = str;
175   while (1)
176     {
177       if (c == ' ' || c == '\n' || c == '\t' || c == '\f' || c == '\r')
178         break;
179       if (c == ':' || c == ')' || c == ']' || c == '"' || c == '/'
180           || c == '(' || c == '[')
181         {
182           ungetc (c, infile);
183           break;
184         }
185       *p++ = c;
186       c = getc (infile);
187     }
188   if (p == str)
189     fatal_with_file_and_line (infile, "missing name or number");
190   if (c == '\n')
191     read_rtx_lineno++;
192
193   *p = 0;
194
195   if (md_constants)
196     {
197       /* Do constant expansion.  */
198       struct md_constant *def;
199
200       p = str;
201       do
202         {
203           struct md_constant tmp_def;
204
205           tmp_def.name = p;
206           def = htab_find (md_constants, &tmp_def);
207           if (def)
208             p = def->value;
209         } while (def);
210       if (p != str)
211         strcpy (str, p);
212     }
213 }
214
215 /* Subroutine of the string readers.  Handles backslash escapes.
216    Caller has read the backslash, but not placed it into the obstack.  */
217 static void
218 read_escape (ob, infile)
219      struct obstack *ob;
220      FILE *infile;
221 {
222   int c = getc (infile);
223
224   switch (c)
225     {
226       /* Backslash-newline is replaced by nothing, as in C.  */
227     case '\n':
228       read_rtx_lineno++;
229       return;
230
231       /* \" \' \\ are replaced by the second character.  */
232     case '\\':
233     case '"':
234     case '\'':
235       break;
236
237       /* Standard C string escapes:
238          \a \b \f \n \r \t \v
239          \[0-7] \x
240          all are passed through to the output string unmolested.
241          In normal use these wind up in a string constant processed
242          by the C compiler, which will translate them appropriately.
243          We do not bother checking that \[0-7] are followed by up to
244          two octal digits, or that \x is followed by N hex digits.
245          \? \u \U are left out because they are not in traditional C.  */
246     case 'a': case 'b': case 'f': case 'n': case 'r': case 't': case 'v':
247     case '0': case '1': case '2': case '3': case '4': case '5': case '6':
248     case '7': case 'x':
249       obstack_1grow (ob, '\\');
250       break;
251
252       /* \; makes stuff for a C string constant containing
253          newline and tab.  */
254     case ';':
255       obstack_grow (ob, "\\n\\t", 4);
256       return;
257
258       /* pass anything else through, but issue a warning.  */
259     default:
260       fprintf (stderr, "%s:%d: warning: unrecognized escape \\%c\n",
261                read_rtx_filename, read_rtx_lineno, c);
262       obstack_1grow (ob, '\\');
263       break;
264     }
265
266   obstack_1grow (ob, c);
267 }
268
269
270 /* Read a double-quoted string onto the obstack.  Caller has scanned
271    the leading quote.  */
272 static char *
273 read_quoted_string (ob, infile)
274      struct obstack *ob;
275      FILE *infile;
276 {
277   int c;
278
279   while (1)
280     {
281       c = getc (infile); /* Read the string  */
282       if (c == '\n')
283         read_rtx_lineno++;
284       else if (c == '\\')
285         {
286           read_escape (ob, infile);
287           continue;
288         }
289       else if (c == '"')
290         break;
291
292       obstack_1grow (ob, c);
293     }
294
295   obstack_1grow (ob, 0);
296   return obstack_finish (ob);
297 }
298
299 /* Read a braced string (a la Tcl) onto the obstack.  Caller has
300    scanned the leading brace.  Note that unlike quoted strings,
301    the outermost braces _are_ included in the string constant.  */
302 static char *
303 read_braced_string (ob, infile)
304      struct obstack *ob;
305      FILE *infile;
306 {
307   int c;
308   int brace_depth = 1;  /* caller-processed */
309
310   obstack_1grow (ob, '{');
311   while (brace_depth)
312     {
313       c = getc (infile); /* Read the string  */
314       if (c == '\n')
315         read_rtx_lineno++;
316       else if (c == '{')
317         brace_depth++;
318       else if (c == '}')
319         brace_depth--;
320       else if (c == '\\')
321         {
322           read_escape (ob, infile);
323           continue;
324         }
325
326       obstack_1grow (ob, c);
327     }
328
329   obstack_1grow (ob, 0);
330   return obstack_finish (ob);
331 }
332
333 /* Read some kind of string constant.  This is the high-level routine
334    used by read_rtx.  It handles surrounding parentheses, leading star,
335    and dispatch to the appropriate string constant reader.  */
336
337 static char *
338 read_string (ob, infile, star_if_braced)
339      struct obstack *ob;
340      FILE *infile;
341      int star_if_braced;
342 {
343   char *stringbuf;
344   int saw_paren = 0;
345   int c;
346
347   c = read_skip_spaces (infile);
348   if (c == '(')
349     {
350       saw_paren = 1;
351       c = read_skip_spaces (infile);
352     }
353
354   if (c == '"')
355     stringbuf = read_quoted_string (ob, infile);
356   else if (c == '{')
357     {
358       if (star_if_braced)
359         obstack_1grow (ob, '*');
360       stringbuf = read_braced_string (ob, infile);
361     }
362   else
363     fatal_with_file_and_line (infile, "expected `\"' or `{', found `%c'", c);
364
365   if (saw_paren)
366     {
367       c = read_skip_spaces (infile);
368       if (c != ')')
369         fatal_expected_char (infile, ')', c);
370     }
371
372   return stringbuf;
373 }
374 \f
375 /* Provide a version of a function to read a long long if the system does
376    not provide one.  */
377 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !defined(HAVE_ATOLL) && !defined(HAVE_ATOQ)
378 HOST_WIDE_INT
379 atoll (p)
380     const char *p;
381 {
382   int neg = 0;
383   HOST_WIDE_INT tmp_wide;
384
385   while (ISSPACE (*p))
386     p++;
387   if (*p == '-')
388     neg = 1, p++;
389   else if (*p == '+')
390     p++;
391
392   tmp_wide = 0;
393   while (ISDIGIT (*p))
394     {
395       HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
396       if (new_wide < tmp_wide)
397         {
398           /* Return INT_MAX equiv on overflow.  */
399           tmp_wide = (~(unsigned HOST_WIDE_INT) 0) >> 1;
400           break;
401         }
402       tmp_wide = new_wide;
403       p++;
404     }
405
406   if (neg)
407     tmp_wide = -tmp_wide;
408   return tmp_wide;
409 }
410 #endif
411
412 /* Given a constant definition, return a hash code for its name.  */
413 static unsigned
414 def_hash (def)
415      const void *def;
416 {
417   unsigned result, i;
418   const char *string = ((const struct md_constant *) def)->name;
419
420   for (result = i = 0;*string++ != '\0'; i++)
421     result += ((unsigned char) *string << (i % CHAR_BIT));
422   return result;
423 }
424
425 /* Given two constant definitions, return true if they have the same name.  */
426 static int
427 def_name_eq_p (def1, def2)
428      const void *def1, *def2;
429 {
430   return ! strcmp (((const struct md_constant *) def1)->name,
431                    ((const struct md_constant *) def2)->name);
432 }
433
434 /* INFILE is a FILE pointer to read text from.  TMP_CHAR is a buffer suitable
435    to read a name or number into.  Process a define_constants directive,
436    starting with the optional space after the "define_constants".  */
437 static void
438 read_constants (infile, tmp_char)
439      FILE *infile;
440      char *tmp_char;
441 {
442   int c;
443   htab_t defs;
444
445   c = read_skip_spaces (infile);
446   if (c != '[')
447     fatal_expected_char (infile, '[', c);
448   defs = md_constants;
449   if (! defs)
450     defs = htab_create (32, def_hash, def_name_eq_p, (htab_del) 0);
451   /* Disable constant expansion during definition processing.  */
452   md_constants = 0;
453   while ( (c = read_skip_spaces (infile)) != ']')
454     {
455       struct md_constant *def;
456       void **entry_ptr;
457
458       if (c != '(')
459         fatal_expected_char (infile, '(', c);
460       def = xmalloc (sizeof (struct md_constant));
461       def->name = tmp_char;
462       read_name (tmp_char, infile);
463       entry_ptr = htab_find_slot (defs, def, TRUE);
464       if (! *entry_ptr)
465         def->name = xstrdup (tmp_char);
466       c = read_skip_spaces (infile);
467       ungetc (c, infile);
468       read_name (tmp_char, infile);
469       if (! *entry_ptr)
470         {
471           def->value = xstrdup (tmp_char);
472           *entry_ptr = def;
473         }
474       else
475         {
476           def = *entry_ptr;
477           if (strcmp (def->value, tmp_char))
478             fatal_with_file_and_line (infile,
479                                       "redefinition of %s, was %s, now %s",
480                                       def->name, def->value, tmp_char);
481         }
482       c = read_skip_spaces (infile);
483       if (c != ')')
484         fatal_expected_char (infile, ')', c);
485     }
486   md_constants = defs;
487   c = read_skip_spaces (infile);
488   if (c != ')')
489     fatal_expected_char (infile, ')', c);
490 }
491
492 /* For every constant definition, call CALLBACK with two arguments:
493    a pointer a pointer to the constant definition and INFO.
494    Stops when CALLBACK returns zero.  */
495 void
496 traverse_md_constants (callback, info)
497      htab_trav callback;
498      void *info;
499 {
500   if (md_constants)
501     htab_traverse (md_constants, callback, info);
502 }
503
504 static void
505 validate_const_int (infile, string)
506      FILE *infile;
507      const char *string;
508 {
509   const char *cp;
510   int valid = 1;
511
512   cp = string;
513   while (*cp && ISSPACE (*cp))
514     cp++;
515   if (*cp == '-' || *cp == '+')
516     cp++;
517   if (*cp == 0)
518     valid = 0;
519   for (; *cp; cp++)
520     if (! ISDIGIT (*cp))
521       valid = 0;
522   if (!valid)
523     fatal_with_file_and_line (infile, "invalid decimal constant \"%s\"\n", string);
524 }
525
526 /* Read an rtx in printed representation from INFILE
527    and return an actual rtx in core constructed accordingly.
528    read_rtx is not used in the compiler proper, but rather in
529    the utilities gen*.c that construct C code from machine descriptions.  */
530
531 rtx
532 read_rtx (infile)
533      FILE *infile;
534 {
535   int i, j;
536   RTX_CODE tmp_code;
537   const char *format_ptr;
538   /* tmp_char is a buffer used for reading decimal integers
539      and names of rtx types and machine modes.
540      Therefore, 256 must be enough.  */
541   char tmp_char[256];
542   rtx return_rtx;
543   int c;
544   int tmp_int;
545   HOST_WIDE_INT tmp_wide;
546
547   /* Obstack used for allocating RTL objects.  */
548   static struct obstack rtl_obstack;
549   static int initialized;
550
551   /* Linked list structure for making RTXs: */
552   struct rtx_list
553     {
554       struct rtx_list *next;
555       rtx value;                /* Value of this node.  */
556     };
557
558   if (!initialized) {
559     obstack_init (&rtl_obstack);
560     initialized = 1;
561   }
562
563 again:
564   c = read_skip_spaces (infile); /* Should be open paren.  */
565   if (c != '(')
566     fatal_expected_char (infile, '(', c);
567
568   read_name (tmp_char, infile);
569
570   tmp_code = UNKNOWN;
571
572   if (! strcmp (tmp_char, "define_constants"))
573     {
574       read_constants (infile, tmp_char);
575       goto again;
576     }
577   for (i = 0; i < NUM_RTX_CODE; i++)
578     if (! strcmp (tmp_char, GET_RTX_NAME (i)))
579       {
580         tmp_code = (RTX_CODE) i;        /* get value for name */
581         break;
582       }
583
584   if (tmp_code == UNKNOWN)
585     fatal_with_file_and_line (infile, "unknown rtx code `%s'", tmp_char);
586
587   /* (NIL) stands for an expression that isn't there.  */
588   if (tmp_code == NIL)
589     {
590       /* Discard the closeparen.  */
591       while ((c = getc (infile)) && c != ')')
592         ;
593
594       return 0;
595     }
596
597   /* If we end up with an insn expression then we free this space below.  */
598   return_rtx = rtx_alloc (tmp_code);
599   format_ptr = GET_RTX_FORMAT (GET_CODE (return_rtx));
600
601   /* If what follows is `: mode ', read it and
602      store the mode in the rtx.  */
603
604   i = read_skip_spaces (infile);
605   if (i == ':')
606     {
607       read_name (tmp_char, infile);
608       for (j = 0; j < NUM_MACHINE_MODES; j++)
609         if (! strcmp (GET_MODE_NAME (j), tmp_char))
610           break;
611
612       if (j == MAX_MACHINE_MODE)
613         fatal_with_file_and_line (infile, "unknown mode `%s'", tmp_char);
614
615       PUT_MODE (return_rtx, (enum machine_mode) j);
616     }
617   else
618     ungetc (i, infile);
619
620   for (i = 0; i < GET_RTX_LENGTH (GET_CODE (return_rtx)); i++)
621     switch (*format_ptr++)
622       {
623         /* 0 means a field for internal use only.
624            Don't expect it to be present in the input.  */
625       case '0':
626         break;
627
628       case 'e':
629       case 'u':
630         XEXP (return_rtx, i) = read_rtx (infile);
631         break;
632
633       case 'V':
634         /* 'V' is an optional vector: if a closeparen follows,
635            just store NULL for this element.  */
636         c = read_skip_spaces (infile);
637         ungetc (c, infile);
638         if (c == ')')
639           {
640             XVEC (return_rtx, i) = 0;
641             break;
642           }
643         /* Now process the vector.  */
644
645       case 'E':
646         {
647           /* Obstack to store scratch vector in.  */
648           struct obstack vector_stack;
649           int list_counter = 0;
650           rtvec return_vec = NULL_RTVEC;
651
652           c = read_skip_spaces (infile);
653           if (c != '[')
654             fatal_expected_char (infile, '[', c);
655
656           /* add expressions to a list, while keeping a count */
657           obstack_init (&vector_stack);
658           while ((c = read_skip_spaces (infile)) && c != ']')
659             {
660               ungetc (c, infile);
661               list_counter++;
662               obstack_ptr_grow (&vector_stack, (PTR) read_rtx (infile));
663             }
664           if (list_counter > 0)
665             {
666               return_vec = rtvec_alloc (list_counter);
667               memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
668                       list_counter * sizeof (rtx));
669             }
670           XVEC (return_rtx, i) = return_vec;
671           obstack_free (&vector_stack, NULL);
672           /* close bracket gotten */
673         }
674         break;
675
676       case 'S':
677         /* 'S' is an optional string: if a closeparen follows,
678            just store NULL for this element.  */
679         c = read_skip_spaces (infile);
680         ungetc (c, infile);
681         if (c == ')')
682           {
683             XSTR (return_rtx, i) = 0;
684             break;
685           }
686
687       case 'T':
688       case 's':
689         {
690           char *stringbuf;
691
692           /* The output template slot of a DEFINE_INSN,
693              DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
694              gets a star inserted as its first character, if it is
695              written with a brace block instead of a string constant.  */
696           int star_if_braced = (format_ptr[-1] == 'T');
697
698           stringbuf = read_string (&rtl_obstack, infile, star_if_braced);
699
700           /* For insn patterns, we want to provide a default name
701              based on the file and line, like "*foo.md:12", if the
702              given name is blank.  These are only for define_insn and
703              define_insn_and_split, to aid debugging.  */
704           if (*stringbuf == '\0'
705               && i == 0
706               && (GET_CODE (return_rtx) == DEFINE_INSN
707                   || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT))
708             {
709               char line_name[20];
710               const char *fn = (read_rtx_filename ? read_rtx_filename : "rtx");
711               const char *slash;
712               for (slash = fn; *slash; slash ++)
713                 if (*slash == '/' || *slash == '\\' || *slash == ':')
714                   fn = slash + 1;
715               obstack_1grow (&rtl_obstack, '*');
716               obstack_grow (&rtl_obstack, fn, strlen (fn));
717               sprintf (line_name, ":%d", read_rtx_lineno);
718               obstack_grow (&rtl_obstack, line_name, strlen (line_name)+1);
719               stringbuf = (char *) obstack_finish (&rtl_obstack);
720             }
721
722           if (star_if_braced)
723             XTMPL (return_rtx, i) = stringbuf;
724           else
725             XSTR (return_rtx, i) = stringbuf;
726         }
727         break;
728
729       case 'w':
730         read_name (tmp_char, infile);
731         validate_const_int (infile, tmp_char);
732 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
733         tmp_wide = atoi (tmp_char);
734 #else
735 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
736         tmp_wide = atol (tmp_char);
737 #else
738         /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
739            But prefer not to use our hand-rolled function above either.  */
740 #if defined(HAVE_ATOLL) || !defined(HAVE_ATOQ)
741         tmp_wide = atoll (tmp_char);
742 #else
743         tmp_wide = atoq (tmp_char);
744 #endif
745 #endif
746 #endif
747         XWINT (return_rtx, i) = tmp_wide;
748         break;
749
750       case 'i':
751       case 'n':
752         read_name (tmp_char, infile);
753         validate_const_int (infile, tmp_char);
754         tmp_int = atoi (tmp_char);
755         XINT (return_rtx, i) = tmp_int;
756         break;
757
758       default:
759         fprintf (stderr,
760                  "switch format wrong in rtl.read_rtx(). format was: %c.\n",
761                  format_ptr[-1]);
762         fprintf (stderr, "\tfile position: %ld\n", ftell (infile));
763         abort ();
764       }
765
766   c = read_skip_spaces (infile);
767   if (c != ')')
768     fatal_expected_char (infile, ')', c);
769
770   return return_rtx;
771 }