xref: /dragonfly/contrib/gcc-4.7/gcc/genhooks.c (revision c37c9ab3)
1 /* Process target.def to create initialization macros definition in
2    target-hooks-def.h and documentation in target-hooks.texi.
3    Copyright (C) 2009, 2010, 2011 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 3, 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 COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20 #include "bconfig.h"
21 #include "system.h"
22 #include "hashtab.h"
23 #include "errors.h"
24 
25 struct hook_desc { const char *doc, *type, *name, *param, *init, *docname; };
26 static struct hook_desc hook_array[] = {
27 #define HOOK_VECTOR_1(NAME, FRAGMENT)	\
28   { 0, 0, #NAME, 0, 0, HOOK_TYPE },
29 #define DEFHOOKPOD(NAME, DOC, TYPE, INIT) \
30   { DOC, #TYPE, HOOK_PREFIX #NAME, 0, #INIT, HOOK_TYPE },
31 #define DEFHOOK(NAME, DOC, TYPE, PARAMS, INIT) \
32   { DOC, #TYPE, HOOK_PREFIX #NAME, #PARAMS, #INIT, HOOK_TYPE },
33 #define DEFHOOK_UNDOC(NAME, DOC, TYPE, PARAMS, INIT) \
34   { "*", #TYPE, HOOK_PREFIX #NAME, #PARAMS, #INIT, HOOK_TYPE },
35 #include "target.def"
36 #include "c-family/c-target.def"
37 #include "common/common-target.def"
38 #undef DEFHOOK
39 };
40 
41 /* For each @Fcode in the first paragraph of the documentation string DOC,
42    print an @findex directive.  HOOK_NAME is the name of the hook this bit of
43    documentation pertains to.  */
44 static void
45 emit_findices (const char *doc, const char *hook_name)
46 {
47   const char *end = strstr (doc, "\n\n");
48   const char *fcode;
49 
50   while ((fcode = strstr (doc, "@Fcode{")) && (!end || fcode < end))
51     {
52       fcode += strlen ("@Fcode{");
53       doc = strchr (fcode, '}');
54       if (!doc)
55 	fatal ("Malformed @Fcode for hook %s\n", hook_name);
56       printf ("@findex %.*s\n", (int) (doc - fcode), fcode);
57       doc = fcode;
58     }
59 }
60 
61 /* Return an upper-case copy of IN.  */
62 static char *
63 upstrdup (const char *in)
64 {
65   char *p, *ret = xstrdup (in);
66   for (p = ret; *p; p++)
67     *p = TOUPPER (*p);
68   return ret;
69 }
70 
71 /* Struct for 'start hooks' which start a sequence of consecutive hooks
72    that are defined in target.def and to be documented in tm.texi.  */
73 struct s_hook
74 {
75   char *name;
76   int pos;
77 };
78 
79 static hashval_t
80 s_hook_hash (const void *p)
81 {
82   const struct s_hook *s_hook = (const struct s_hook *)p;
83   return htab_hash_string (s_hook->name);
84 }
85 
86 static int
87 s_hook_eq_p (const void *p1, const void *p2)
88 {
89   return (strcmp (((const struct s_hook *) p1)->name,
90 		  ((const struct s_hook *) p2)->name) == 0);
91 }
92 
93 /* Read the documentation file with name IN_FNAME, perform substitutions
94    to incorporate informtion from hook_array, and emit the result on stdout.
95    Hooks defined with DEFHOOK / DEFHOOKPOD are emitted at the place of a
96    matching @hook in the input file; if there is no matching @hook, the
97    hook is emitted after the hook that precedes it in target.def .
98    Usually, the emitted hook documentation starts with the hook
99    signature, followed by the string from the doc field.
100    The documentation is bracketed in @deftypefn / @deftypevr and a matching
101    @end.
102    While emitting the doc field, @Fcode is translated to @code, and an
103    @findex entry is added to the affected paragraph.
104    If the doc field starts with '*', the leading '*' is stripped, and the doc
105    field is otherwise emitted unaltered; no function signature/
106    @deftypefn/deftypevr/@end is emitted.
107    In particular, a doc field of "*" means not to emit any ocumentation for
108    this target.def / hook_array entry at all (there might be documentation
109    for this hook in the file named IN_FNAME, though).
110    A doc field of 0 is used to append the hook signature after the previous
111    hook's signture, so that one description can be used for a group of hooks.
112    When the doc field is "", @deftypefn/@deftypevr and the hook signature
113    is emitted, but not the matching @end.  This allows all the free-form
114    documentation to be placed in IN_FNAME, to work around GPL/GFDL
115    licensing incompatibility issues.  */
116 static void
117 emit_documentation (const char *in_fname)
118 {
119   int i, j;
120   char buf[1000];
121   htab_t start_hooks = htab_create (99, s_hook_hash, s_hook_eq_p, (htab_del) 0);
122   FILE *f;
123   bool found_start = false;
124 
125   /* Enter all the start hooks in start_hooks.  */
126   f = fopen (in_fname, "r");
127   if (!f)
128     {
129       perror ("");
130       fatal ("Couldn't open input file");
131     }
132   while (fscanf (f, "%*[^@]"), buf[0] = '\0',
133 	 fscanf (f, "@%5[^ \n]", buf) != EOF)
134     {
135       void **p;
136       struct s_hook *shp;
137 
138       if (strcmp (buf, "hook") != 0)
139 	continue;
140       buf[0] = '\0';
141       fscanf (f, "%999s", buf);
142       shp = XNEW (struct s_hook);
143       shp->name = upstrdup (buf);
144       shp->pos = -1;
145       p = htab_find_slot (start_hooks, shp, INSERT);
146       if (*p != HTAB_EMPTY_ENTRY)
147 	fatal ("Duplicate placement for hook %s\n", shp->name);
148       *(struct s_hook **) p = shp;
149     }
150   fclose (f);
151   /* For each hook in hook_array, if it is a start hook, store its position.  */
152   for (i = 0; i < (int) (sizeof hook_array / sizeof hook_array[0]); i++)
153     {
154       struct s_hook sh, *shp;
155       void *p;
156 
157       if (!hook_array[i].doc || strcmp (hook_array[i].doc, "*") == 0)
158 	continue;
159       sh.name = upstrdup (hook_array[i].name);
160       p = htab_find (start_hooks, &sh);
161       if (p)
162 	{
163 	  shp = (struct s_hook *) p;
164 	  if (shp->pos >= 0)
165 	    fatal ("Duplicate hook %s\n", sh.name);
166 	  shp->pos = i;
167 	  found_start = true;
168 	}
169       else if (!found_start)
170 	fatal ("No place specified to document hook %s\n", sh.name);
171       free (sh.name);
172     }
173   /* Copy input file to stdout, substituting @hook directives with the
174      corresponding hook documentation sequences.  */
175   f = fopen (in_fname, "r");
176   if (!f)
177     {
178       perror ("");
179       fatal ("Couldn't open input file");
180     }
181   for (;;)
182     {
183       struct s_hook sh, *shp;
184       int c = getc (f);
185       char *name;
186 
187       if (c == EOF)
188 	break;
189       if (c != '@')
190 	{
191 	  putchar (c);
192 	  continue;
193 	}
194       buf[0] = '\0';
195       fscanf (f, "%5[^ \n]", buf);
196       if (strcmp (buf, "hook") != 0)
197 	{
198 	  printf ("@%s", buf);
199 	  continue;
200 	}
201       fscanf (f, "%999s", buf);
202       sh.name = name = upstrdup (buf);
203       shp = (struct s_hook *) htab_find (start_hooks, &sh);
204       if (!shp || shp->pos < 0)
205 	fatal ("No documentation for hook %s\n", sh.name);
206       i = shp->pos;
207       do
208 	{
209 	  const char *q, *e;
210 	  const char *deftype;
211 	  const char *doc, *fcode, *p_end;
212 
213 	  /* A leading '*' means to output the documentation string without
214 	     further processing.  */
215 	  if (*hook_array[i].doc == '*')
216 	    printf ("%s", hook_array[i].doc + 1);
217 	  else
218 	    {
219 	      if (i != shp->pos)
220 		printf ("\n\n");
221 	      emit_findices (hook_array[i].doc, name);
222 
223 	      /* Print header.  Function-valued hooks have a parameter list,
224 		 unlike POD-valued ones.  */
225 	      deftype = hook_array[i].param ? "deftypefn" : "deftypevr";
226 	      printf ("@%s {%s} ", deftype, hook_array[i].docname);
227 	      if (strchr (hook_array[i].type, ' '))
228 		printf ("{%s}", hook_array[i].type);
229 	      else
230 		printf ("%s", hook_array[i].type);
231 	      printf (" %s", name);
232 	      if (hook_array[i].param)
233 		{
234 		  /* Print the parameter list, with the parameter names
235 		     enclosed in @var{}.  */
236 		  printf (" ");
237 		  for (q = hook_array[i].param; (e = strpbrk (q, " *,)"));
238 		       q = e + 1)
239 		    /* Type names like 'int' are followed by a space, sometimes
240 		       also by '*'.  'void' should appear only in "(void)".  */
241 		    if (*e == ' ' || *e == '*' || *q == '(')
242 		      printf ("%.*s", (int) (e - q + 1), q);
243 		    else
244 		      printf ("@var{%.*s}%c", (int) (e - q), q, *e);
245 		}
246 	      /* POD-valued hooks sometimes come in groups with common
247 		 documentation.*/
248 	      for (j = i + 1;
249 		   j < (int) (sizeof hook_array / sizeof hook_array[0])
250 		   && hook_array[j].doc == 0 && hook_array[j].type; j++)
251 		{
252 		  char *namex = upstrdup (hook_array[j].name);
253 
254 		  printf ("\n@%sx {%s} {%s} %s",
255 			  deftype, hook_array[j].docname,
256 			  hook_array[j].type, namex);
257 		}
258 	      if (hook_array[i].doc[0])
259 		{
260 		  printf ("\n");
261 		  /* Print each documentation paragraph in turn.  */
262 		  for (doc = hook_array[i].doc; *doc; doc = p_end)
263 		    {
264 		      /* Find paragraph end.  */
265 		      p_end = strstr (doc, "\n\n");
266 		      p_end = (p_end ? p_end + 2 : doc + strlen (doc));
267 		      /* Print paragraph, emitting @Fcode as @code.  */
268 		      for (; (fcode = strstr (doc, "@Fcode{")) && fcode < p_end;
269 			   doc = fcode + 2)
270 			printf ("%.*s@", (int) (fcode - doc), doc);
271 		      printf ("%.*s", (int) (p_end - doc), doc);
272 		      /* Emit function indices for next paragraph.  */
273 		      emit_findices (p_end, name);
274 		    }
275 		  printf ("\n@end %s", deftype);
276 		}
277 	    }
278 	  if (++i >= (int) (sizeof hook_array / sizeof hook_array[0])
279 	      || !hook_array[i].doc)
280 	    break;
281 	  free (name);
282 	  sh.name = name = upstrdup (hook_array[i].name);
283 	}
284       while (!htab_find (start_hooks, &sh));
285       free (name);
286     }
287 }
288 
289 /* Emit #defines to stdout (this will be redirected to generate
290    target-hook-def.h) which set target hooks initializer macros
291    to their default values.  These should only be emitted for hooks
292    whose type is given by DOCNAME.  */
293 static void
294 emit_init_macros (const char *docname)
295 {
296   int i;
297   const int MAX_NEST = 2;
298   int print_nest, nest = 0;
299 
300   for (print_nest = 0; print_nest <= MAX_NEST; print_nest++)
301     {
302       for (i = 0; i < (int) (sizeof hook_array / sizeof hook_array[0]); i++)
303 	{
304 	  char *name = upstrdup (hook_array[i].name);
305 
306 	  if (strcmp (hook_array[i].docname, docname) != 0)
307 	    continue;
308 
309 	  if (!hook_array[i].type)
310 	    {
311 	      if (*name)
312 		{
313 		  if (nest && nest == print_nest)
314 		    printf ("    %s, \\\n", name);
315 		  nest++;
316 		  if (nest > MAX_NEST)
317 		    fatal ("Unexpected nesting of %s\n", name);
318 		  if (nest == print_nest)
319 		    printf ("\n#define %s \\\n  { \\\n", name);
320 		}
321 	      else
322 		{
323 		  if (nest == print_nest)
324 		    printf ("  }\n");
325 		  nest--;
326 		}
327 	      continue;
328 	    }
329 	  if (0 == print_nest)
330 	    {
331 	      /* Output default definitions of target hooks.  */
332 	      printf ("#ifndef %s\n#define %s %s\n#endif\n",
333 		      name, name, hook_array[i].init);
334 	    }
335 	  if (nest == print_nest)
336 	    printf ("    %s, \\\n", name);
337 	}
338     }
339 }
340 
341 int
342 main (int argc, char **argv)
343 {
344   if (argc >= 3)
345     emit_documentation (argv[2]);
346   else
347     emit_init_macros (argv[1]);
348   return 0;
349 }
350