OSDN Git Service

* i386/i386.h (CPP_486_SPEC, CPP_586_SPEC, CPP_686_SPEC): New specs.
[pf3gnuchains/gcc-fork.git] / gcc / gen-protos.c
1 /* gen-protos.c - massages a list of prototypes, for use by fixproto.
2    Copyright (C) 1993, 94-96, 1998 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
7 later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #include "hconfig.h"
19 #include "system.h"
20 #include "gansidecl.h"
21 #include "scan.h"
22 #include "cpplib.h"
23 #include "cpphash.h"
24
25 int verbose = 0;
26 char *progname;
27
28 #define HASH_SIZE 2503 /* a prime */
29 int hash_tab[HASH_SIZE];
30 int next_index;
31
32 int
33 hashf (name, len, hashsize)
34      register const U_CHAR *name;
35      register int len;
36      int hashsize;
37 {
38   register int r = 0;
39
40   while (len--)
41     r = HASHSTEP (r, *name++);
42
43   return MAKE_POS (r) % hashsize;
44 }
45
46 static void
47 add_hash (fname)
48      char *fname;
49 {
50   int i, i0;
51
52   /* NOTE:  If you edit this, also edit lookup_std_proto in fix-header.c !! */
53   i = hashf (fname, strlen (fname), HASH_SIZE);
54   i0 = i;
55   if (hash_tab[i] != 0)
56     {
57       for (;;)
58         {
59           i = (i+1) % HASH_SIZE;
60           if (i == i0)
61             abort ();
62           if (hash_tab[i] == 0)
63             break;
64         }
65     }
66   hash_tab[i] = next_index;
67
68   next_index++;
69 }
70
71 /* Given a function prototype, fill in the fields of FN.
72    The result is a boolean indicating if a function prototype was found.
73
74    The input string is modified (trailing NULs are inserted).
75    The fields of FN point to the input string.  */
76
77 static int
78 parse_fn_proto (start, end, fn)
79      char *start, *end;
80      struct fn_decl *fn;
81 {
82   register char *ptr;
83   int param_nesting = 1;
84   char *param_start, *param_end, *decl_start, *name_start, *name_end;
85
86   ptr = end - 1;
87   while (*ptr == ' ' || *ptr == '\t') ptr--;
88   if (*ptr-- != ';')
89     {
90       fprintf (stderr, "Funny input line: %s\n", start);
91       return 0;
92     }
93   while (*ptr == ' ' || *ptr == '\t') ptr--;
94   if (*ptr != ')')
95     {
96       fprintf (stderr, "Funny input line: %s\n", start);
97       return 0;
98     }
99   param_end = ptr;
100   for (;;)
101     {
102       int c = *--ptr;
103       if (c == '(' && --param_nesting == 0)
104         break;
105       else if (c == ')')
106         param_nesting++;
107     }
108   param_start = ptr+1;
109
110   ptr--;
111   while (*ptr == ' ' || *ptr == '\t') ptr--;
112
113   if (!ISALNUM (*ptr))
114     {
115       if (verbose)
116         fprintf (stderr, "%s: Can't handle this complex prototype: %s\n",
117                  progname, start);
118       return 0;
119     }
120   name_end = ptr+1;
121
122   while (ISALNUM (*ptr) || *ptr == '_') --ptr;
123   name_start = ptr+1;
124   while (*ptr == ' ' || *ptr == '\t') ptr--;
125   ptr[1] = 0;
126   *param_end = 0;
127   *name_end = 0;
128
129   decl_start = start;
130   if (strncmp (decl_start, "typedef ", 8) == 0)
131     return 0;
132   if (strncmp (decl_start, "extern ", 7) == 0)
133     decl_start += 7;
134
135   fn->fname = name_start;
136   fn->rtype = decl_start;
137   fn->params = param_start;
138   return 1;
139 }
140
141 int
142 main (argc, argv)
143      int argc;
144      char **argv;
145 {
146   FILE *inf = stdin;
147   FILE *outf = stdout;
148   int i;
149   sstring linebuf;
150   struct fn_decl fn_decl;
151
152   i = strlen (argv[0]);
153   while (i > 0 && argv[0][i-1] != '/') --i;
154   progname = &argv[0][i];
155
156   INIT_SSTRING (&linebuf);
157
158   fprintf (outf, "struct fn_decl std_protos[] = {\n");
159
160   /* A hash table entry of 0 means "unused" so reserve it.  */
161   fprintf (outf, "  {\"\", \"\", \"\"},\n");
162   next_index = 1;
163   
164   for (;;)
165     {
166       int c = skip_spaces (inf, ' ');
167
168       if (c == EOF)
169         break;
170       linebuf.ptr = linebuf.base;
171       ungetc (c, inf);
172       c = read_upto (inf, &linebuf, '\n');
173       if (linebuf.base[0] == '#') /* skip cpp command */
174         continue;
175       if (linebuf.base[0] == '\0') /* skip empty line */
176         continue;
177
178       if (! parse_fn_proto (linebuf.base, linebuf.ptr, &fn_decl))
179         continue;
180
181       add_hash (fn_decl.fname);
182
183       fprintf (outf, "  {\"%s\", \"%s\", \"%s\"},\n",
184                fn_decl.fname, fn_decl.rtype, fn_decl.params);
185
186       if (c == EOF)
187         break;
188     }
189   fprintf (outf, "  {0, 0, 0}\n};\n");
190
191
192   fprintf (outf, "#define HASH_SIZE %d\n", HASH_SIZE);
193   fprintf (outf, "short hash_tab[HASH_SIZE] = {\n");
194   for (i = 0; i < HASH_SIZE; i++)
195     fprintf (outf, "  %d,\n", hash_tab[i]);
196   fprintf (outf, "};\n");
197
198   return 0;
199 }
200
201 /* Avoid error if config defines abort as fancy_abort.
202    It's not worth "really" implementing this because ordinary
203    compiler users never run fix-header.  */
204
205 void
206 fancy_abort ()
207 {
208   abort ();
209 }
210
211 void
212 fatal (s)
213      char *s;
214 {
215   fprintf (stderr, "%s: %s\n", "gen-protos", s);
216   exit (FATAL_EXIT_CODE);
217 }