]>
Commit | Line | Data |
---|---|---|
7936052f | 1 | /* gen-protos.c - massages a list of prototypes, for use by fixproto. |
3b708058 | 2 | Copyright (C) 1993, 1994, 1995, 1996, 1998, |
fe9565ed | 3 | 1999, 2003, 2004, 2005 Free Software Foundation, Inc. |
7936052f PB |
4 | |
5 | This program is free software; you can redistribute it and/or modify it | |
6 | under the terms of the GNU General Public License as published by the | |
7 | Free Software Foundation; either version 2, or (at your option) any | |
8 | later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
366ccddb | 17 | Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
7936052f | 18 | |
4977bab6 | 19 | #include "bconfig.h" |
b04cd507 | 20 | #include "system.h" |
4977bab6 ZW |
21 | #include "coretypes.h" |
22 | #include "tm.h" | |
7936052f | 23 | #include "scan.h" |
a140c081 | 24 | #include "errors.h" |
7936052f | 25 | |
3d348653 | 26 | int verbose = 0; |
3d348653 | 27 | |
3d7aafde AJ |
28 | static void add_hash (const char *); |
29 | static int parse_fn_proto (char *, char *, struct fn_decl *); | |
ca3075bd | 30 | |
7936052f | 31 | #define HASH_SIZE 2503 /* a prime */ |
3d348653 DE |
32 | int hash_tab[HASH_SIZE]; |
33 | int next_index; | |
5fa7f88c | 34 | int collisions; |
7936052f | 35 | |
3d348653 | 36 | static void |
3d7aafde | 37 | add_hash (const char *fname) |
3d348653 DE |
38 | { |
39 | int i, i0; | |
7936052f | 40 | |
3d348653 | 41 | /* NOTE: If you edit this, also edit lookup_std_proto in fix-header.c !! */ |
5fa7f88c | 42 | i = hashstr (fname, strlen (fname)) % HASH_SIZE; |
3d348653 DE |
43 | i0 = i; |
44 | if (hash_tab[i] != 0) | |
45 | { | |
5fa7f88c | 46 | collisions++; |
3d348653 DE |
47 | for (;;) |
48 | { | |
49 | i = (i+1) % HASH_SIZE; | |
b2d59f6f | 50 | gcc_assert (i != i0); |
3d348653 DE |
51 | if (hash_tab[i] == 0) |
52 | break; | |
53 | } | |
54 | } | |
55 | hash_tab[i] = next_index; | |
7936052f | 56 | |
3d348653 DE |
57 | next_index++; |
58 | } | |
629b20e2 | 59 | |
3d348653 DE |
60 | /* Given a function prototype, fill in the fields of FN. |
61 | The result is a boolean indicating if a function prototype was found. | |
62 | ||
63 | The input string is modified (trailing NULs are inserted). | |
64 | The fields of FN point to the input string. */ | |
65 | ||
66 | static int | |
3d7aafde | 67 | parse_fn_proto (char *start, char *end, struct fn_decl *fn) |
629b20e2 | 68 | { |
b3694847 | 69 | char *ptr; |
3d348653 DE |
70 | int param_nesting = 1; |
71 | char *param_start, *param_end, *decl_start, *name_start, *name_end; | |
72 | ||
73 | ptr = end - 1; | |
74 | while (*ptr == ' ' || *ptr == '\t') ptr--; | |
75 | if (*ptr-- != ';') | |
76 | { | |
77 | fprintf (stderr, "Funny input line: %s\n", start); | |
78 | return 0; | |
79 | } | |
80 | while (*ptr == ' ' || *ptr == '\t') ptr--; | |
81 | if (*ptr != ')') | |
82 | { | |
83 | fprintf (stderr, "Funny input line: %s\n", start); | |
84 | return 0; | |
85 | } | |
86 | param_end = ptr; | |
87 | for (;;) | |
88 | { | |
89 | int c = *--ptr; | |
90 | if (c == '(' && --param_nesting == 0) | |
91 | break; | |
92 | else if (c == ')') | |
93 | param_nesting++; | |
94 | } | |
95 | param_start = ptr+1; | |
96 | ||
97 | ptr--; | |
98 | while (*ptr == ' ' || *ptr == '\t') ptr--; | |
99 | ||
88dab4f6 | 100 | if (!ISALNUM ((unsigned char)*ptr)) |
3d348653 DE |
101 | { |
102 | if (verbose) | |
103 | fprintf (stderr, "%s: Can't handle this complex prototype: %s\n", | |
104 | progname, start); | |
105 | return 0; | |
106 | } | |
107 | name_end = ptr+1; | |
108 | ||
0df6c2c7 KG |
109 | while (ISIDNUM (*ptr)) |
110 | --ptr; | |
3d348653 DE |
111 | name_start = ptr+1; |
112 | while (*ptr == ' ' || *ptr == '\t') ptr--; | |
113 | ptr[1] = 0; | |
114 | *param_end = 0; | |
115 | *name_end = 0; | |
116 | ||
117 | decl_start = start; | |
118 | if (strncmp (decl_start, "typedef ", 8) == 0) | |
119 | return 0; | |
120 | if (strncmp (decl_start, "extern ", 7) == 0) | |
121 | decl_start += 7; | |
122 | ||
123 | fn->fname = name_start; | |
124 | fn->rtype = decl_start; | |
125 | fn->params = param_start; | |
126 | return 1; | |
629b20e2 RS |
127 | } |
128 | ||
7936052f | 129 | int |
8e2b6930 | 130 | main (int argc ATTRIBUTE_UNUSED, char **argv) |
7936052f PB |
131 | { |
132 | FILE *inf = stdin; | |
133 | FILE *outf = stdout; | |
3d348653 DE |
134 | int i; |
135 | sstring linebuf; | |
3d348653 | 136 | struct fn_decl fn_decl; |
7936052f | 137 | |
03cad97d | 138 | i = strlen (argv[0]); |
a36cf2bb JW |
139 | while (i > 0 && argv[0][i-1] != '/') --i; |
140 | progname = &argv[0][i]; | |
03cad97d | 141 | |
98a3dad4 | 142 | /* Unlock the stdio streams. */ |
2653bb0c | 143 | unlock_std_streams (); |
98a3dad4 | 144 | |
3d348653 DE |
145 | INIT_SSTRING (&linebuf); |
146 | ||
7936052f PB |
147 | fprintf (outf, "struct fn_decl std_protos[] = {\n"); |
148 | ||
3d348653 | 149 | /* A hash table entry of 0 means "unused" so reserve it. */ |
88dab4f6 | 150 | fprintf (outf, " {\"\", \"\", \"\", 0},\n"); |
3d348653 | 151 | next_index = 1; |
3d7aafde | 152 | |
7936052f PB |
153 | for (;;) |
154 | { | |
155 | int c = skip_spaces (inf, ' '); | |
3d348653 | 156 | |
7936052f PB |
157 | if (c == EOF) |
158 | break; | |
159 | linebuf.ptr = linebuf.base; | |
160 | ungetc (c, inf); | |
161 | c = read_upto (inf, &linebuf, '\n'); | |
162 | if (linebuf.base[0] == '#') /* skip cpp command */ | |
163 | continue; | |
164 | if (linebuf.base[0] == '\0') /* skip empty line */ | |
165 | continue; | |
166 | ||
3d348653 | 167 | if (! parse_fn_proto (linebuf.base, linebuf.ptr, &fn_decl)) |
7936052f | 168 | continue; |
7936052f | 169 | |
3d348653 | 170 | add_hash (fn_decl.fname); |
7936052f | 171 | |
88dab4f6 | 172 | fprintf (outf, " {\"%s\", \"%s\", \"%s\", 0},\n", |
3d348653 | 173 | fn_decl.fname, fn_decl.rtype, fn_decl.params); |
7936052f PB |
174 | |
175 | if (c == EOF) | |
176 | break; | |
177 | } | |
88dab4f6 | 178 | fprintf (outf, " {0, 0, 0, 0}\n};\n"); |
7936052f PB |
179 | |
180 | ||
181 | fprintf (outf, "#define HASH_SIZE %d\n", HASH_SIZE); | |
182 | fprintf (outf, "short hash_tab[HASH_SIZE] = {\n"); | |
183 | for (i = 0; i < HASH_SIZE; i++) | |
184 | fprintf (outf, " %d,\n", hash_tab[i]); | |
185 | fprintf (outf, "};\n"); | |
186 | ||
5fa7f88c ZW |
187 | fprintf (stderr, "gen-protos: %d entries %d collisions\n", |
188 | next_index, collisions); | |
3d7aafde | 189 | |
7936052f PB |
190 | return 0; |
191 | } |