Patchwork gengtype improvements for plugins, thirdround! patch 1/7 [declprog]

login
register
mail settings
Submitter Basile Starynkevitch
Date Sept. 21, 2010, 7:03 p.m.
Message ID <20100921210301.d92889be.basile@starynkevitch.net>
Download mbox | patch
Permalink /patch/65369/
State New
Headers show

Comments

Basile Starynkevitch - Sept. 21, 2010, 7:03 p.m.
Hello All,

[join work by Basile Starynkevitch & Jeremie Salvucci]

References: http://gcc.gnu.org/ml/gcc-patches/2010-08/msg02058.html 
http://gcc.gnu.org/ml/gcc-patches/2010-08/msg02050.html
http://gcc.gnu.org/ml/gcc-patches/2010-08/msg02051.html
http://gcc.gnu.org/ml/gcc-patches/2010-09/msg00616.html
http://gcc.gnu.org/ml/gcc-patches/2010-09/msg00744.html

I am submitting the third round of a serie of patches on gengtype,
provind some significant code clean-up & ability to persist the state
of gengtype to enable running gengtype for plugin use without requiring
them to have both GCC build and source trees.
The mails are improved versions of previous emails, and the patches are
improved versions of previously sent patches. I tried hard to have well
indented & well commented patches, and I did try to take into account
all the previous remarks.

I really hope to get an "OK", or at least an "OK with those changes".

I also hope the patches will be committed before end of stage 1 for 4.6
i.e. end of october 2010


Our patch serie is as before made of seven pieces.

patch 1 [declprog], like
http://gcc.gnu.org/ml/gcc-patches/2010-09/msg00663.html moves many
private definitions, types, functions from gengtype.c to gengtype.h &
provide a GNU friendly gengtype program invocation. We have to declare
publicly most internal types of gengtype since we really want the
persistent machinery to be in a separate file gengtype-state.c and
because we feel that gengtype.c is messy and big enough.

patch 2 [verbosity], like
http://gcc.gnu.org/ml/gcc-patches/2010-09/msg00682.html  add a verbose
flag -v to the gengtype program. When given (it can be given more than
once to increase verbosity), it explains what gengtype is doing. This
flag should be useful to any gengtype user.

patch 3 [inputfile], like
http://gcc.gnu.org/ml/gcc-patches/2010-09/msg00842.html provides an
input_file structure remove the horrible disgusting hack of encoding
the set of languages in a bitmap in four bytes before the file path.
Honestly, I cannot understand why the old gengtype had such an horrible
coding, and how could it have been accepted in the first place, nearly
nine or ten years ago.

patch 4 [filesrules] like
http://gcc.gnu.org/ml/gcc-patches/2010-09/msg00983.html provides a
better machinery to associate an output file to each input_file. So it
is improving the get_output_file_with_visibility function with a rule &
regular-expression based machinery.  I did thought a few seconds about
adding a convention in comments -for instance requiring that gimple.h
has a comment /*@@ GENGTYPEOUTPUT gt-gimple.h */, and tree.h have /*@@
GENGTYPEOUTPUT gt-tree.h */ etc, and have gengtype parse such comments.
However, such a patch would require me to patch, by adding just a
comment, almost every file of GCC, and I am sadly pretty sure nobody
will review such a work; the social rules of GCC make such an idea
impractical.

patch 5 [typedopt] like
http://gcc.gnu.org/ml/gcc-patches/2010-09/msg01017.html is making each
option a disciminated union. This is needed to be able to persist
gengtype state. 

patch 6 [wstate] see
http://gcc.gnu.org/ml/gcc-patches/2010-09/msg01029.html gives the
gengtype-state.c which has all the persistency machinery and enable its
use.

patch 7 [doc] improves the documentation gty.texi see
http://gcc.gnu.org/ml/gcc-patches/2010-09/msg01032.html



The first piece [declprog] of our series of patches moves many private
definitions, types, functions from gengtype.c to gengtype.h & provide
a GNU friendly gengtype program invocation. Since a later patch of the
same serie implement the state persistency in a separate file
gengtype-state.c (see our future patch 6/N[wstate]) a lot of variables
& types which used to be internal to gengtype.c are now publicly
available in gengtype.h.


We did incorporate Paolo Bonzini's & Ralf Wildenhues' suggestions on
build/version.o in Makefile.in from
http://gcc.gnu.org/ml/gcc-patches/2010-08/msg02114.html &
http://gcc.gnu.org/ml/gcc-patches/2010-08/msg02050.html

We did improve the help message as suggested by Laurynas Biveinis in
http://gcc.gnu.org/ml/gcc-patches/2010-09/msg00207.html and in
http://gcc.gnu.org/ml/gcc-patches/2010-09/msg00744.html

Please notice an important point in this patch (and other patches of
this serie): we program defensively, since we feel that gengtype is
very difficult to understand.  In particular, we added several
debugging tricks, notably the DBGPRINTF & DBGPRINT_COUNT_TYPE macros,
whose sole side-effect is to produce debugging output when gengtype is
given the --debug (or -D) program argument.  This feature (only
available when ENABLE_CHECKING is configured) is deemed necessary
because gengtype code is really difficult to grasp and to debug.  It
is complementary to the dump_everything & friends functions already
incorporated into gengtype and contributed by Laurynas Biveinis:
Laurynas dump feature is useful not only to debug and enhance gengtype
itself (a not trivial task, believe us!) but may also be useful by
gengtype users (e.g. plugins developers).  In contrast, our DBGPRINTF
... macros are mostly useful to debug & understand the processing of
gengtype itself.  Of course, we could remove them, but we strongly
feel that they could be useful to some other gengtype hacker (the same
can be said of Laurynas dump facility).  So to the attention of the
allmighty reviewers: please measure the pro & conses before asking us
to remove these DBGPRINTF macros.  We really hope they will stay... 

################################################################
gcc/ChangeLog entry:

2010-09-20  Jeremie Salvucci  <jeremie.salvucci@free.fr>
	    Basile Starynkevitch  <basile@starynkevitch.net>

	* gengtype.c:  Include getopt.h and version.h.

	(lang_bitmap, struct outf, outf_p)
	(get_output_file_with_visibility, oprintf): Definitions moved to
	gengtype.h
	(output_files, header_file, srcdir, srcdir_len, this_file,
do_dump): No more static variables.
	(do_debug): New.
	(dbgprint_count_type_at): Added new function.
	(gengtype_long_options): New.
	(print_usage, print_version, parse_program_options): New.
	(main): Call parse_program_options, and removed old option
	handling code.  Added some debug output.

	* gengtype.h:  Updated copyright year.
	(lang_bitmap, struct outf, outf_p, header_file, oprintf)
	(get_output_file_with_visibility, srcdir, srcdir_len, do_dump):
	Moved from gengtype.c to here.
	(do_debug, read_state_filename, write_state_filename): New
variables. (DBGPRINTF, DBGPRINT_COUNT_TYPE): New macros.

	* Makefile.in:
	(REVISION): Always defined.
	(version.o): Removed ifdef REVISION_c.
	(s-gtype): Pass arguments to build/gengtype program.
	(build/version.o): Added building rule.
	(build/gengtype$(build_exeext)): Added build/version.o.
################################################################

the diff file against trunk rev 164437.

Ok for trunk? or Ok with what changes?

Regards.
Laurynas Biveinis - Sept. 22, 2010, 1:57 a.m.
The indentation of the patch is still (glaringly) wrong in a
significant number of places.

-/* the generated plugin output name & file */
+
+/* the generated plugin output file and name.  */

The

+static void
+print_usage (void)
+{
+    printf ("Usage: %s\n", progname);

Wrong indentation.

+/* Parse the program options using getopt_long... */
+static void
+parse_program_options (int argc, char**argv)
+{
+    int opt = -1;
+    while ((opt = getopt_long (argc, argv, "hVdP:S:I:w:r:D",
+			       gengtype_long_options, NULL)) >= 0)
+    {
+	switch (opt)
+	{
+	case 'h': /* --help */
+	    print_usage ();
+	    break;
+	case 'V': /* --version */
+	    print_version ();
+	    break;
+	case 'd': /* --dump */
+      do_dump = 1;
+	    break;

Wrong indentation...

+  /*** Parse the input list and the input files.  ***/

/* Parse ... */

+      for (i = 0; i < num_gt_files; i++) {
+	parse_file (gt_files[i]);
+	DBGPRINTF ("parsed file #%d %s", (int) i, gt_files[i]);
+      }

Wrong formatting.

+      if (nb_plugin_files <= 0 || !plugin_files)
+	fatal ("No plugin files given in plugin mode for %s", plugin_output_filename);

+      /* Parse our plugin files.  */
+      for (ix = 0; ix < nb_plugin_files; ix++)
+	parse_file (plugin_files[ix]);
+
+      if (hit_error)
+	return 1;
+
+      plugin_output = create_file ("GCC", plugin_output_filename);
+      DBGPRINTF ("created plugin_output %p named %s",
+		 (void*) plugin_output, plugin_output->name);
+    }
+  else
+    { /* No plugin files, we are in normal mode.  */
+      if (!srcdir)
+	fatal ("gengtype needs a source directory in normal mode");
+    }

Wrong formatting.

+  /* The call to set_gc_used may indirectly call find_param_structure
+     hence enlarge the param_structs list of types.  So it should
+     happen before writing the state.  */
   set_gc_used (variables);

Can you explain this comment? I mean, *all* source processing things
must happen before writing the state, so isn't the comment too
obvious?

+#endif /*ENABLE_CHECKING*/

Space after /* and before */

> gcc/ChangeLog entry:
>
> 2010-09-20  Jeremie Salvucci  <jeremie.salvucci@free.fr>
>            Basile Starynkevitch  <basile@starynkevitch.net>
>
>        * gengtype.c:  Include getopt.h and version.h.
>
>        (lang_bitmap, struct outf, outf_p)
>        (get_output_file_with_visibility, oprintf): Definitions moved to
>        gengtype.h
>        (output_files, header_file, srcdir, srcdir_len, this_file,
> do_dump): No more static variables.
>        (do_debug): New.
>        (dbgprint_count_type_at): Added new function.
>        (gengtype_long_options): New.
>        (print_usage, print_version, parse_program_options): New.
>        (main): Call parse_program_options, and removed old option
>        handling code.  Added some debug output.
>
>        * gengtype.h:  Updated copyright year.
>        (lang_bitmap, struct outf, outf_p, header_file, oprintf)
>        (get_output_file_with_visibility, srcdir, srcdir_len, do_dump):
>        Moved from gengtype.c to here.
>        (do_debug, read_state_filename, write_state_filename): New
> variables. (DBGPRINTF, DBGPRINT_COUNT_TYPE): New macros.
>
>        * Makefile.in:
>        (REVISION): Always defined.
>        (version.o): Removed ifdef REVISION_c.
>        (s-gtype): Pass arguments to build/gengtype program.
>        (build/version.o): Added building rule.
>        (build/gengtype$(build_exeext)): Added build/version.o.

Patch

Index: gcc/gengtype.c
===================================================================
--- gcc/gengtype.c	(revision 164437)
+++ gcc/gengtype.c	(working copy)
@@ -20,10 +20,12 @@ 
 
 #include "bconfig.h"
 #include "system.h"
-#include "gengtype.h"
 #include "errors.h"	/* for fatal */
+#include "getopt.h"
 #include "double-int.h"
+#include "version.h"    /* for version_string & pkgversion_string.  */
 #include "hashtab.h"
+#include "gengtype.h"
 
 /* Data types, macros, etc. used only in this file.  */
 
@@ -39,7 +41,6 @@  enum typekind {
   TYPE_PARAM_STRUCT
 };
 
-typedef unsigned lang_bitmap;
 
 /* A way to pass data through to the output end.  */
 struct options
@@ -120,47 +121,42 @@  struct type
   || (x)->kind == TYPE_STRUCT 			\
   || (x)->kind == TYPE_LANG_STRUCT)
 
-/* Structure representing an output file.  */
-struct outf
-{
-  struct outf *next;
-  const char *name;
-  size_t buflength;
-  size_t bufused;
-  char *buf;
-};
-typedef struct outf * outf_p;
 
-/* An output file, suitable for definitions, that can see declarations
-   made in INPUT_FILE and is linked into every language that uses
-   INPUT_FILE.  May return NULL in plugin mode. */
-extern outf_p get_output_file_with_visibility
-   (const char *input_file);
+
 const char *get_output_file_name (const char *);
 
-/* Print, like fprintf, to O.  No-op if O is NULL. */
-static void oprintf (outf_p o, const char *S, ...)
-     ATTRIBUTE_PRINTF_2;
 
 /* The list of output files.  */
-static outf_p output_files;
+outf_p output_files;
 
+/* The output header file that is included into pretty much every
+   source file.  */
+outf_p header_file;
+
+
+/* The name of the file containing the list of input files.  */
+static char* inputlist;
+
 /* The plugin input files and their number; in that case only
    a single file is produced.  */
 static char** plugin_files;
 static size_t nb_plugin_files;
-/* the generated plugin output name & file */
+
+/* the generated plugin output file and name.  */
 static outf_p plugin_output;
+static char* plugin_output_filename;
 
-/* The output header file that is included into pretty much every
-   source file.  */
-static outf_p header_file;
+/* Our source directory and its length.  */
+const char *srcdir;
+size_t srcdir_len;
 
-/* Source directory.  */
-static const char *srcdir;
+/* Variables used for reading and writing the state.  */
+const char *read_state_filename;
+const char *write_state_filename;
 
-/* Length of srcdir name.  */
-static size_t srcdir_len = 0;
+/* Variables to help debugging.  */
+int do_dump;
+int do_debug;
 
 static outf_p create_file (const char *, const char *);
 
@@ -220,10 +216,12 @@  xasprintf (const char *format, ...)
 static const char **gt_files;
 static size_t num_gt_files;
 
-/* A number of places use the name of this file for a location for
-   things that we can't rely on the source to define.  Make sure we
-   can still use pointer comparison on filenames.  */
-static const char this_file[] = __FILE__;
+/* A number of places use the name of this "gengtype.h" file for a
+   location for things that we can't rely on the source to define.
+   Make sure we can still use pointer comparison on filenames.  */
+const char this_file[] = __FILE__;
+/* The "system.h" file is likewise specially useful.  */
+const char system_h_file[] = "system.h";
 
 /* Vector of per-language directories.  */
 static const char **lang_dir_names;
@@ -249,8 +247,10 @@  static lang_bitmap
 get_lang_bitmap (const char *gtfile)
 {
 
-  if (gtfile == this_file)
-    /* Things defined in this file are universal.  */
+  if (gtfile == this_file || gtfile == system_h_file)
+    /* Things defined in this "gengtype.c" file or in "system.h" are
+       universal (and there is no space for their lang_bitmap before
+       their file names).  */
     return (((lang_bitmap)1) << num_lang_dirs) - 1;
   else
     {
@@ -275,6 +275,67 @@  set_lang_bitmap (char *gtfile, lang_bitmap n)
     }
 }
 
+
+#if ENABLE_CHECKING
+/* Utility debugging function, printing the various type counts within
+   a list of types.  Called thru the DBGPRINT_COUNT_TYPE macro.  */
+void dbgprint_count_type_at (const char*fil, int lin, const char*msg,  type_p t)
+{
+  int nb_types=0, nb_scalar=0, nb_string=0;
+  int nb_struct=0, nb_union=0, nb_array=0, nb_pointer=0;
+  int nb_lang_struct=0, nb_param_struct=0;
+  type_p p=NULL;
+  for (p=t; p; p=p->next)
+    {
+      nb_types++;
+      switch (p->kind)
+	{
+	case TYPE_SCALAR:
+	  nb_scalar++;
+	  break;
+	case TYPE_STRING:
+	  nb_string++;
+	  break;
+	case TYPE_STRUCT:
+	  nb_struct++;
+	  break;
+	case TYPE_UNION:
+	  nb_union++;
+	  break;
+	case TYPE_POINTER:
+	  nb_pointer++;
+	  break;
+	case TYPE_ARRAY:
+	  nb_array++;
+	  break;
+	case TYPE_LANG_STRUCT:
+	  nb_lang_struct++;
+	  break;
+	case TYPE_PARAM_STRUCT:
+	  nb_param_struct++;
+	  break;
+	default:
+	  gcc_unreachable ();
+	}
+    }
+  fprintf (stderr, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
+	   lbasename (fil), lin, msg, nb_types);
+  if (nb_scalar>0 || nb_string>0)
+    fprintf (stderr, "@@%%@@ %d scalars, %d strings\n",
+	     nb_scalar, nb_string);
+  if (nb_struct>0 || nb_union>0)
+    fprintf (stderr, "@@%%@@ %d structs, %d unions\n",
+	     nb_struct, nb_union);
+  if (nb_pointer>0 || nb_array>0)
+    fprintf (stderr, "@@%%@@ %d pointers, %d arrays\n",
+	     nb_pointer, nb_array);
+  if (nb_lang_struct>0 || nb_param_struct>0)
+    fprintf (stderr, "@@%%@@ %d lang_structs, %d param_structs\n",
+	     nb_lang_struct, nb_param_struct);
+  fprintf (stderr, "\n");
+}
+#endif /*ENABLE_CHECKING*/
+
 /* Scan the input file, LIST, and determine how much space we need to
    store strings in.  Also, count the number of language directories
    and files.  The numbers returned are overestimates as they does not
@@ -4209,91 +4270,258 @@  dump_everything (void)
 }
 
 
+
+/* Option specification for getopt_long.  */
+static const struct option gengtype_long_options[] = {
+  { "help",      no_argument, NULL, 'h' },
+  { "version",   no_argument, NULL, 'V' },
+  { "dump",      no_argument, NULL, 'd' },
+  { "debug",     no_argument, NULL, 'D' },
+  { "plugin",    required_argument, NULL, 'P' },
+  { "srcdir",    required_argument, NULL, 'S' },
+  { "inputs",    required_argument, NULL, 'I' },
+  { "read-state",    required_argument, NULL, 'r' },
+  { "write-state",    required_argument, NULL, 'w' },
+  /* Terminating NULL placeholder.  */
+  { NULL,        no_argument, NULL, 0   },
+};
+
+
+static void
+print_usage (void)
+{
+    printf ("Usage: %s\n", progname);
+    printf ("\t -h | --help "
+	    " \t# Give this help.\n");
+    printf ("\t -D | --debug "
+	    " \t# Give debug output to debug %s itself.\n",
+            progname);
+    printf ("\t -V | --version "
+	    " \t# Give version information.\n");
+    printf ("\t -d | --dump "
+	    " \t# Dump state for debugging.\n");
+    printf ("\t -P | --plugin <output-file> <plugin-src> ... "
+            " \t# Generate for plugin.\n");
+    printf ("\t -S | --srcdir <GCC-directory> "
+            " \t# Specify the GCC source directory.\n");
+    printf ("\t -I | --inputs <input-list> "
+            " \t# Specify the file with source files list.\n");
+    printf ("\t -w | --write-state <state-file> "
+            " \t# Write a state file.\n");
+    printf ("\t -r | --read-state <state-file> "
+            " \t# Read a state file.\n");
+}
+
+static void
+print_version (void)
+{
+    printf ("%s %s%s\n", progname, pkgversion_string, version_string);
+    printf ("Report bugs: %s\n", bug_report_url);
+}
+
+/* Parse the program options using getopt_long... */
+static void
+parse_program_options (int argc, char**argv)
+{
+    int opt = -1;
+    while ((opt = getopt_long (argc, argv, "hVdP:S:I:w:r:D",
+			       gengtype_long_options, NULL)) >= 0)
+    {
+	switch (opt)
+	{
+	case 'h': /* --help */
+	    print_usage ();
+	    break;
+	case 'V': /* --version */
+	    print_version ();
+	    break;
+	case 'd': /* --dump */
+      do_dump = 1;
+	    break;
+	case 'D': /* --debug */
+	    do_debug = 1;
+	    break;
+	case 'P': /* --plugin */
+	    if (optarg)
+		plugin_output_filename = optarg;
+	    else
+		fatal ("missing plugin output file name");
+	    break;
+	case 'S': /* --srcdir */
+	    if (optarg)
+		srcdir = optarg;
+	    else
+		fatal ("missing source directory");
+	    srcdir_len = strlen (srcdir);
+	    break;
+	case 'I': /* --inputs */
+	    if (optarg)
+		inputlist = optarg;
+	    else
+		fatal ("missing input list");
+	    break;
+	case 'r': /* --read-state */
+	    if (optarg)
+		read_state_filename = optarg;
+	    else
+		fatal ("missing read state file");
+	    DBGPRINTF ("read state %s\n", optarg);
+	    break;
+	case 'w': /* --write-state */
+	    DBGPRINTF ("write state %s\n", optarg);
+	    if (optarg)
+		write_state_filename = optarg;
+	    else
+		fatal ("missing write state file");
+	    break;
+	default:
+	    fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
+	    print_usage ();
+	    fatal ("unexpected flag");
+    }
+    };
+    if (plugin_output_filename)
+    {
+	/* In plugin mode we require some input files.  */
+	int i = 0;
+	if (optind >= argc)
+	    fatal ("no source files given in plugin mode");
+	nb_plugin_files = argc - optind;
+	for (i = 0; i < (int) nb_plugin_files; i++)
+      {
+	    char *name = argv[i + optind];
+	    plugin_files[i] = name;
+      }
+    }
+}
+
+
 int
 main (int argc, char **argv)
 {
   size_t i;
-  static struct fileloc pos = { this_file, 0 };
-  char* inputlist = 0;
-  int do_dump = 0;
+  static struct fileloc pos = { NULL, 0 };
   outf_p output_header;
-  char* plugin_output_filename = NULL;
-  /* fatal uses this */
-  progname = "gengtype";
 
-  if (argc >= 2 && !strcmp (argv[1], "-d"))
+  /* Mandatory common initializations.  */
+  progname = "gengtype"; /* For fatal and messages.  */
+  /* Set the scalar_is_char union number for predefined scalar types.  */
+  scalar_nonchar.u.scalar_is_char = FALSE;
+  scalar_char.u.scalar_is_char = TRUE;
+
+  parse_program_options (argc, argv);
+
+#if ENABLE_CHECKING
+  if (do_debug)
     {
-      do_dump = 1;
-      argv = &argv[1];
-      argc--;
+      time_t now = (time_t) 0;
+      time (&now);
+      DBGPRINTF ("gengtype started pid %d at %s",
+		 (int) getpid (), ctime (&now));
     }
+#endif
 
-  if (argc >= 6 && !strcmp (argv[1], "-P"))
+  /*** Parse the input list and the input files.  ***/
+  DBGPRINTF ("inputlist %s", inputlist);
+  if (read_state_filename)
     {
-      plugin_output_filename = argv[2];
-      plugin_output = create_file ("GCC", plugin_output_filename);
-      srcdir = argv[3];
-      inputlist = argv[4];
-      nb_plugin_files = argc - 5;
-      plugin_files = XCNEWVEC (char *, nb_plugin_files);
-      for (i = 0; i < nb_plugin_files; i++)
-      {
-        /* Place an all zero lang_bitmap before the plugin file
-	   name.  */
-        char *name = argv[i + 5];
-        int len = strlen(name) + 1 + sizeof (lang_bitmap);
-        plugin_files[i] = XCNEWVEC (char, len) + sizeof (lang_bitmap);
-        strcpy (plugin_files[i], name);
-      }
+      fatal ("read state %s not implemented yet", read_state_filename);
+      /* TODO: implement read state.  */
     }
-  else if (argc == 3)
+  else if (inputlist)
     {
-      srcdir = argv[1];
-      inputlist = argv[2];
+      /* These types are set up with #define or else outside of where
+	 we can see them.  We should initialize them before calling
+	 read_input_list.  */
+      pos.file = this_file;
+      pos.line = __LINE__ + 1;
+      do_scalar_typedef ("CUMULATIVE_ARGS", &pos); pos.line++;
+      do_scalar_typedef ("REAL_VALUE_TYPE", &pos); pos.line++;
+      do_scalar_typedef ("FIXED_VALUE_TYPE", &pos); pos.line++;
+      do_scalar_typedef ("double_int", &pos); pos.line++;
+      do_scalar_typedef ("uint64_t", &pos); pos.line++;
+      do_scalar_typedef ("uint8", &pos); pos.line++;
+      do_scalar_typedef ("jword", &pos); pos.line++;
+      do_scalar_typedef ("JCF_u2", &pos); pos.line++;
+      do_scalar_typedef ("void", &pos); pos.line++;
+      do_typedef ("PTR", create_pointer (resolve_typedef ("void", &pos)), &pos);
+      read_input_list (inputlist);
+      for (i = 0; i < num_gt_files; i++) {
+	parse_file (gt_files[i]);
+	DBGPRINTF ("parsed file #%d %s", (int) i, gt_files[i]);
+      }
+      DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
+      DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs);
+
     }
   else
-    fatal ("usage: gengtype [-d] [-P pluginout.h] srcdir input-list "
-           "[file1 file2 ... fileN]");
-
-  srcdir_len = strlen (srcdir);
-
-  read_input_list (inputlist);
+    fatal ("either an input list or a read state file should be given");
   if (hit_error)
     return 1;
 
-  scalar_char.u.scalar_is_char = true;
-  scalar_nonchar.u.scalar_is_char = false;
-  gen_rtx_next ();
 
-  /* These types are set up with #define or else outside of where
-     we can see them.  */
-  pos.line = __LINE__ + 1;
-  do_scalar_typedef ("CUMULATIVE_ARGS", &pos); pos.line++;
-  do_scalar_typedef ("REAL_VALUE_TYPE", &pos); pos.line++;
-  do_scalar_typedef ("FIXED_VALUE_TYPE", &pos); pos.line++;
-  do_scalar_typedef ("double_int", &pos); pos.line++;
-  do_scalar_typedef ("uint64_t", &pos); pos.line++;
-  do_scalar_typedef ("uint8", &pos); pos.line++;
-  do_scalar_typedef ("jword", &pos); pos.line++;
-  do_scalar_typedef ("JCF_u2", &pos); pos.line++;
-  do_scalar_typedef ("void", &pos); pos.line++;
-  do_typedef ("PTR", create_pointer (resolve_typedef ("void", &pos)), &pos);
+  if (plugin_output_filename)
+    {
+      size_t ix = 0;
+      /* In plugin mode, we should have read a state file, and have
+	 given at least one plugin file.  */
+      if (!read_state_filename)
+	fatal ("No read state given in plugin mode for %s", plugin_output_filename);
 
-  for (i = 0; i < num_gt_files; i++)
-    parse_file (gt_files[i]);
+      if (nb_plugin_files <= 0 || !plugin_files)
+	fatal ("No plugin files given in plugin mode for %s", plugin_output_filename);
 
+      /* Parse our plugin files.  */
+      for (ix = 0; ix < nb_plugin_files; ix++)
+	parse_file (plugin_files[ix]);
+
+      if (hit_error)
+	return 1;
+
+      plugin_output = create_file ("GCC", plugin_output_filename);
+      DBGPRINTF ("created plugin_output %p named %s",
+		 (void*) plugin_output, plugin_output->name);
+    }
+  else
+    { /* No plugin files, we are in normal mode.  */
+      if (!srcdir)
+	fatal ("gengtype needs a source directory in normal mode");
+    }
   if (hit_error)
     return 1;
 
+  gen_rtx_next ();
+
+  /* The call to set_gc_used may indirectly call find_param_structure
+     hence enlarge the param_structs list of types.  So it should
+     happen before writing the state.  */
   set_gc_used (variables);
 
+  /* We should write the state here, but it is not yet implemented.  */
+  if (write_state_filename)
+    {
+      fatal ("write state %s in not yet implemented", write_state_filename);
+      /* TODO: implement write state.  */
+    }
+
+
   open_base_files ();
+
   write_enum_defn (structures, param_structs);
   write_typed_alloc_defns (structures, typedefs);
   output_header = plugin_output ? plugin_output : header_file;
+  DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
+                       structures);
+  DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
+                       param_structs);
+
   write_types (output_header, structures, param_structs, &ggc_wtd);
   if (plugin_files == NULL)
     {
+      DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
+                           structures);
+      DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
+                           param_structs);
       write_types (header_file, structures, param_structs, &pch_wtd);
       write_local (header_file, structures, param_structs);
     }
@@ -4305,12 +4533,7 @@  main (int argc, char **argv)
   if (do_dump)
     dump_everything ();
 
-  if (plugin_files)
-  {
-    for (i = 0; i < nb_plugin_files; i++)
-      free (plugin_files[i] - sizeof (lang_bitmap));
-    free (plugin_files);
-  }
+  /* Don't bother about free-ing any input file, etc.  */
 
   if (hit_error)
     return 1;
Index: gcc/gengtype.h
===================================================================
--- gcc/gengtype.h	(revision 164437)
+++ gcc/gengtype.h	(working copy)
@@ -1,5 +1,6 @@ 
 /* Process source files and output type information.
-   Copyright (C) 2002, 2003, 2004, 2007, 2008 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2003, 2004, 2007, 2008, 2010
+   Free Software Foundation, Inc.
 
 This file is part of GCC.
 
@@ -20,6 +21,10 @@  along with GCC; see the file COPYING3.  If not see
 #ifndef GCC_GENGTYPE_H
 #define GCC_GENGTYPE_H
 
+/* Sets of accepted source languages like C, C++, Ada... are
+   represented by a bitmap.  */
+typedef unsigned lang_bitmap;
+
 /* A file position, mostly for error messages.
    The FILE element may be compared using pointer equality.  */
 struct fileloc {
@@ -37,6 +42,44 @@  typedef struct options *options_p;
 extern int lexer_toplevel_done;
 extern struct fileloc lexer_line;
 
+/* Structure representing an output file.  */
+struct outf
+{
+  struct outf *next;
+  const char *name;
+  size_t buflength;
+  size_t bufused;
+  char *buf;
+};
+typedef struct outf* outf_p;
+
+/* The list of output files.  */
+extern outf_p output_files;
+
+/* The output header file that is included into pretty much every
+   source file.  */
+extern outf_p header_file;
+
+/* Print, like fprintf, to O.  No-op if O is NULL.  */
+void oprintf (outf_p o, const char *S, ...)
+     ATTRIBUTE_PRINTF_2;
+
+/* An output file, suitable for definitions, that can see declarations
+   made in INPUT_FILE and is linked into every language that uses
+   INPUT_FILE.  May return NULL in plugin mode.  */
+extern outf_p get_output_file_with_visibility
+   (const char *input_file);
+
+/* Source directory.  */
+extern const char *srcdir;
+
+/* Length of srcdir name.  */
+extern size_t srcdir_len;
+
+/* Variable used for reading and writing the state.  */
+extern const char *read_state_filename;
+extern const char *write_state_filename;
+
 /* Print an error message.  */
 extern void error_at_line
   (const struct fileloc *pos, const char *msg, ...) ATTRIBUTE_PRINTF_2;
@@ -110,4 +153,22 @@  enum {
      a meaningful value to be printed.  */
   FIRST_TOKEN_WITH_VALUE = PARAM_IS
 };
+
+
+/* For debugging purposes of gengtype itself!  */
+extern int do_dump;
+extern int do_debug;
+
+#if ENABLE_CHECKING
+#define DBGPRINTF(Fmt,...) do {if (do_debug)				\
+	    fprintf (stderr, "%s:%d: " Fmt "\n",			\
+		    lbasename (__FILE__),__LINE__, ##__VA_ARGS__);} while (0)
+void dbgprint_count_type_at (const char*, int, const char*, type_p);
+#define DBGPRINT_COUNT_TYPE(Msg,Ty) do {if (do_debug) \
+      dbgprint_count_type_at (__FILE__, __LINE__, Msg, Ty);}while (0)
+#else
+#define DBGPRINTF(Fmt,...) do {/*nodbgrintf*/} while (0)
+#define DBGPRINT_COUNT_TYPE(Msg,Ty) do{/*nodbgprint_count_type*/}while (0)
+#endif /*ENABLE_CHECKING*/
+
 #endif
Index: gcc/Makefile.in
===================================================================
--- gcc/Makefile.in	(revision 164437)
+++ gcc/Makefile.in	(working copy)
@@ -837,6 +837,7 @@  DATESTAMP_c := $(shell cat $(DATESTAMP))
 
 ifeq (,$(wildcard $(REVISION)))
 REVISION_c  :=
+REVISION    :=
 else
 REVISION_c  := $(shell cat $(REVISION))
 endif
@@ -2245,11 +2246,7 @@  gcc-options.o: options.c $(CONFIG_H) $(SYSTEM_H) c
 
 dumpvers: dumpvers.c
 
-ifdef REVISION_c
 version.o: version.c version.h $(REVISION) $(DATESTAMP) $(BASEVER) $(DEVPHASE)
-else
-version.o: version.c version.h $(DATESTAMP) $(BASEVER) $(DEVPHASE)
-endif
 	$(COMPILER) $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) \
 	-DBASEVER=$(BASEVER_s) -DDATESTAMP=$(DATESTAMP_s) \
 	-DREVISION=$(REVISION_s) \
@@ -3809,7 +3806,7 @@  s-gtyp-input: Makefile
 
 s-gtype: build/gengtype$(build_exeext) $(filter-out [%], $(GTFILES)) \
 	 gtyp-input.list
-	$(RUN_GEN) build/gengtype$(build_exeext) $(srcdir) gtyp-input.list
+	$(RUN_GEN) build/gengtype$(build_exeext) -S $(srcdir) -I gtyp-input.list
 	$(STAMP) s-gtype
 
 generated_files = config.h tm.h $(TM_P_H) $(TM_H) multilib.h \
@@ -3829,6 +3826,16 @@  build/%.o :  # dependencies provided by explicit r
 	$(COMPILER_FOR_BUILD) -c $(BUILD_COMPILERFLAGS) $(BUILD_CPPFLAGS) \
 		-o $@ $<
 
+## build/version.o is compiled by the $(COMPILER_FOR_BUILD) but needs
+## several C macro definitions, just like version.o
+build/version.o:  version.c version.h \
+                  $(REVISION) $(DATESTAMP) $(BASEVER) $(DEVPHASE)
+	$(COMPILER_FOR_BUILD) -c $(BUILD_COMPILERFLAGS) $(BUILD_CPPFLAGS) \
+	-DBASEVER=$(BASEVER_s) -DDATESTAMP=$(DATESTAMP_s) \
+	-DREVISION=$(REVISION_s) \
+	-DDEVPHASE=$(DEVPHASE_s) -DPKGVERSION=$(PKGVERSION_s) \
+	-DBUGURL=$(BUGURL_s) -o $@ $<
+
 # Header dependencies for the programs that generate source code.
 # These are library modules...
 build/errors.o : errors.c $(BCONFIG_H) $(SYSTEM_H) errors.h
@@ -3940,7 +3947,8 @@  $(genprog:%=build/gen%$(build_exeext)): $(BUILD_ER
 build/genautomata$(build_exeext) : BUILD_LIBS += -lm
 
 # These programs are not linked with the MD reader.
-build/gengtype$(build_exeext) : build/gengtype-lex.o build/gengtype-parse.o
+build/gengtype$(build_exeext) : build/gengtype-lex.o build/gengtype-parse.o \
+              build/version.o
 build/genhooks$(build_exeext) : $(BUILD_ERRORS)
 
 # Generated source files for gengtype.