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