Patchwork gengtype plugin improvement lastround - patch 1 [declprog]

login
register
mail settings
Submitter Basile Starynkevitch
Date Sept. 24, 2010, 6:25 p.m.
Message ID <20100924202518.42420b16.basile@starynkevitch.net>
Download mbox | patch
Permalink /patch/65675/
State New
Headers show

Comments

Basile Starynkevitch - Sept. 24, 2010, 6:25 p.m.
Hello All,

After http://gcc.gnu.org/ml/gcc-patches/2010-09/msg01739.html &
http://gcc.gnu.org/ml/gcc-patches/2010-09/msg01957.html (and every
other email referenced from them). I am taking into account Laurynas'
comments.

So I will send one patch, only after the previous one has been committed.

Patch relative to trunk r164605.

#### gcc/ChangeLog entry
2010-09-24  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.
################################################################

Ok for trunk? With what changes?

Cheers.
Basile Starynkevitch - Sept. 24, 2010, 6:30 p.m.
On Fri, 24 Sep 2010 20:25:18 +0200
Basile Starynkevitch <basile@starynkevitch.net> wrote:

> Hello All,
> 
> After http://gcc.gnu.org/ml/gcc-patches/2010-09/msg01739.html &
> http://gcc.gnu.org/ml/gcc-patches/2010-09/msg01957.html (and every
> other email referenced from them). I am taking into account Laurynas'
> comments.
> 
> So I will send one patch, only after the previous one has been committed.


In the just sent
http://gcc.gnu.org/ml/gcc-patches/2010-09/msg01964.html message 
> +  /* 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?

I forgot to tell that I clarified that comment.  I was trying to explain
that set_gc_used not only set the gc_used flags of types, but also had
other side effects. The
http://gcc.gnu.org/ml/gcc-patches/2010-09/msg01964.html patch has now
the following comment.  I hope it is helpful.

+  /* The call to set_gc_used may indirectly call find_param_structure
+     hence enlarge the param_structs list of types.  */
   set_gc_used (variables);

Cheers.
Laurynas Biveinis - Sept. 27, 2010, 1:46 a.m.
+  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;

I suggest adding { } here.

+/* 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)
+	{

The last { has one space of indentation too few.

Also in the same function:

+  if (plugin_output_filename)
+    {

Ditto

+      for (i = 0; i < (int) nb_plugin_files; i++)
+	{

Ditto

As far as I'm concerned (I don't have the approval authority), the
patch is OK with these little issues fixed.

> 2010-09-24  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.
Basile Starynkevitch - Sept. 27, 2010, 5:48 a.m.
On Mon, 27 Sep 2010 04:46:45 +0300
Laurynas Biveinis <laurynas.biveinis@gmail.com> wrote:

> +  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;
> 
> I suggest adding { } here.


Are you sure of that?  I thought that the stylistic convention is to
avoid braces for blocks of only one instruction! (and I toke time to
enforce that in the last code patch, that is in patch 6 [wstate]?

What is the preferred style:
   if (some_cond (x))
      return x;

or
   if (some_cond (x))
     {
        return x;
     }

or even
   if (some_cond (x))
      z = call_fun (x, y);

or
   if (some_cond (x))
     {
        z = call_fun (x, y);
     }
   
??
> 
> +    {
> +      switch (opt)
> +	{
> 
> The last { has one space of indentation too few.

There might have been a tab character involved. (I should check).


Laurynas, thanks for your patient reviews.
I'm still waiting an Ok from a person with approval authority!

Cheers
Laurynas Biveinis - Sept. 27, 2010, 5:57 a.m.
2010/9/27 Basile Starynkevitch <basile@starynkevitch.net>:
> On Mon, 27 Sep 2010 04:46:45 +0300
> Laurynas Biveinis <laurynas.biveinis@gmail.com> wrote:
>
>> +  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;
>>
>> I suggest adding { } here.
>
>
> Are you sure of that?  I thought that the stylistic convention is to
> avoid braces for blocks of only one instruction! (and I toke time to
> enforce that in the last code patch, that is in patch 6 [wstate]?

What I mean here is that I'm _not_ suggesting braces for

if (foo)
  bar;
blah;

but rather for multi-line single-statement part:

IMHO

if (foo)
  {
    /* blah */
    bar;
    /* blah */
  }
baz;

is better than

if (foo)
  /* blah */
  bar;
  /* blah */
baz;

because it spans multiple lines and it is not readily apparent that
it's only a single statement.
Basile Starynkevitch - Sept. 29, 2010, 5:47 p.m.
Hello All,

I am pinging http://gcc.gnu.org/ml/gcc-patches/2010-09/msg01964.html

I was hoping an ok from a reviewer, provided I address the cosmetic
changes wanted by Laurynas (who cannot OK my patch)
http://gcc.gnu.org/ml/gcc-patches/2010-09/msg02086.html - cosmetic
since only adding braces or spaces.

And I have no idea if a reviewer prefer my patch with Laurynas cosmetic
changes I don't know what is the best thing to do. I could indeed
change my patch and post a slightly revised variant of it. But I am
afraid to lose interest from potential reviewers (those rare people
able to say Ok).

I have no idea if our (= by me Basile & Jeremie Salvucci) gengtype
improvemnts will make into GCC 4.6.

I am attending the GCC summit, and I will arrive on Saturday october
23rd evening [I probably will be very jet-lagged so probably will be
able to work only after Sunday noon], so if some GCC reviewer is at
Ottawa on Sunday october 24th 2010, he could Ok my patches face to face.

But the more I am waiting for any reviewer to comment (that is say "OK
with such & such changes"), the more I am becoming quite pessimistic
about chances of the gengtype improvements to make into 4.6.

Do you have any constructive suggestiong about getting a reviewer
(Laurynas is not a reviewer, and he is nearly the only person who
commented our patches) interested by our patch series.

Should I contact in private some global reviewer? Who among them is
interested in gengtype?

I am quite afraid that if I send more tiny improvements to our gengtype
patches (-mostly to please Laurynas) any potential reviewer won't even
take time to read the patches. I am afraid that sending more patches
will distract any reviewer's interest and Laurynas cannot Ok my patches,
since he is not a reviewer, however he did say in
http://gcc.gnu.org/ml/gcc-patches/2010-09/msg02086.html "As far as I'm
concerned (I don't have the approval authority), the patch is OK with
these little issues fixed."

Since any improvement I could make is only cosmetic, I don't see the
point of sending the rest of the patch as long as the
http://gcc.gnu.org/ml/gcc-patches/2010-09/msg01964.html patch with the
cosmetic improvements in
http://gcc.gnu.org/ml/gcc-patches/2010-09/msg02086.html is not commited.

And obviously, my efforts for the GCC summit are going prioritarily to
MELT and the MELT tutorial I will give there.

So I have no idea of how to increase the chances of gengtype
improvement go into GCC 4.6.  Any constructive suggestions, in
particular by people who know very well some reviewers who might be
interested in Oking a gengtype patch, is welcome. The last thing I want
to do is to bother (i.e. disturb) too much these nice people. I have no
idea of what is the appropriate ping frequency.

Cheers.

Patch

Index: gcc/gengtype.c
===================================================================
--- gcc/gengtype.c	(revision 164605)
+++ 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.  */
 
@@ -40,7 +42,6 @@  enum typekind
   TYPE_PARAM_STRUCT
 };
 
-typedef unsigned lang_bitmap;
 
 /* A way to pass data through to the output end.  */
 struct options
@@ -117,55 +118,50 @@  struct type
 };
 
 #define UNION_P(x)					\
-  ((x)->kind == TYPE_UNION || 				\
-   ((x)->kind == TYPE_LANG_STRUCT 			\
-    && (x)->u.s.lang_struct->kind == TYPE_UNION))
+ ((x)->kind == TYPE_UNION || 				\
+  ((x)->kind == TYPE_LANG_STRUCT 			\
+   && (x)->u.s.lang_struct->kind == TYPE_UNION))
 #define UNION_OR_STRUCT_P(x)			\
-  ((x)->kind == TYPE_UNION 			\
-   || (x)->kind == TYPE_STRUCT 			\
-   || (x)->kind == TYPE_LANG_STRUCT)
+ ((x)->kind == TYPE_UNION 			\
+  || (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 *);
 
@@ -225,10 +221,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;
@@ -254,8 +252,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
     {
@@ -280,6 +280,65 @@  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
@@ -864,7 +923,7 @@  create_field_at (pair_p next, type_p type, const c
 /* Create a fake field with the given type and name.  NEXT is the next
    field in the chain.  */
 #define create_field(next,type,name) \
-  create_field_all(next,type,name, 0, this_file, __LINE__)
+    create_field_all(next,type,name, 0, this_file, __LINE__)
 
 /* Like create_field, but the field is only valid when condition COND
    is true.  */
@@ -893,7 +952,7 @@  create_optional_field_ (pair_p next, type_p type,
 }
 
 #define create_optional_field(next,type,name,cond)	\
-  create_optional_field_(next,type,name,cond,__LINE__)
+       create_optional_field_(next,type,name,cond,__LINE__)
 
 /* Reverse a linked list of 'struct pair's in place.  */
 pair_p
@@ -3230,13 +3289,13 @@  write_field_root (outf_p f, pair_p v, type_p type,
 /* Write out to F the table entry and any marker routines needed to
    mark NAME as TYPE.  V can be one of three values:
 
-   - null, if NAME is too complex to represent using a single
-   count and stride.  In this case, it is an error for NAME to
-   contain any gc-ed data.
+     - null, if NAME is too complex to represent using a single
+       count and stride.  In this case, it is an error for NAME to
+       contain any gc-ed data.
 
-   - the outermost array that contains NAME, if NAME is part of an array.
+     - the outermost array that contains NAME, if NAME is part of an array.
 
-   - the C variable that contains NAME, if NAME is not part of an array.
+     - the C variable that contains NAME, if NAME is not part of an array.
 
    LINE is the line of the C source that declares the root variable.
    HAS_LENGTH is nonzero iff V was a variable-length array.  IF_MARKED
@@ -3732,7 +3791,7 @@  note_def_vec (const char *type_name, bool is_scala
    we had expanded the macros in vec.h:
 
    typedef struct VEC_<type>_<astrat> {
-   VEC_<type>_base base;
+     VEC_<type>_base base;
    } VEC_<type>_<astrat>;
 */
 void
@@ -4223,100 +4282,264 @@  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	/* ENABLE_CHECKING */
 
-  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++)
+      fatal ("read state %s not implemented yet", read_state_filename);
+      /* TODO: implement read state.  */
+    }
+  else if (inputlist)
+    {
+      /* 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++)
 	{
-	  /* 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);
+	  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 if (argc == 3)
-    {
-      srcdir = argv[1];
-      inputlist = argv[2];
-    }
   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.  */
   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);
     }
@@ -4328,12 +4551,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 or plugin file, etc.  */
 
   if (hit_error)
     return 1;
Index: gcc/gengtype.h
===================================================================
--- gcc/gengtype.h	(revision 164605)
+++ 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 @@ 
 #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
@@ -38,14 +43,50 @@  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;	/* (-S) program argument. */
+
+/* Length of srcdir name.  */
+extern size_t srcdir_len;
+
+/* Variable used for reading and writing the state.  */
+extern const char *read_state_filename; /* (-r) program argument. */
+extern const char *write_state_filename; /* (-w) program argument. */
+
 /* Print an error message.  */
 extern void error_at_line
 (const struct fileloc *pos, const char *msg, ...) ATTRIBUTE_PRINTF_2;
 
 /* Like asprintf, but calls fatal() on out of memory.  */
-extern char *
-xasprintf (const char *, ...)
-  ATTRIBUTE_PRINTF_1;
+extern char *xasprintf (const char *, ...) ATTRIBUTE_PRINTF_1;
 
 /* Constructor routines for types.  */
 extern void do_typedef (const char *s, type_p t, struct fileloc *pos);
@@ -117,4 +158,28 @@  enum
        a meaningful value to be printed.  */
     FIRST_TOKEN_WITH_VALUE = PARAM_IS
   };
+
+
+/* For debugging purposes we provide two flags.  */
+
+/* Dump everything to understand gengtype's state. Might be useful to
+   gengtype users.  */
+extern int do_dump;		/* (-d) program argument. */
+
+/* Trace the execution by many DBGPRINTF (with the position inside
+   gengtype source code).  Only useful to debug gengtype itself.  */
+extern int do_debug;		/* (-D) program argument. */
+
+#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 164605)
+++ 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
@@ -1469,11 +1470,13 @@  OBJS-archive = \
 
 OBJS = $(OBJS-common) $(OBJS-md) $(OBJS-archive)
 
+OBJS-onestep = libbackend.o $(OBJS-archive)
+
 # This lists all host objects for the front ends.
 ALL_HOST_FRONTEND_OBJS = $(C_OBJS) \
   $(foreach v,$(CONFIG_LANGUAGES),$($(v)_OBJS))
 
-ALL_HOST_BACKEND_OBJS = $(GCC_OBJS) $(OBJS) \
+ALL_HOST_BACKEND_OBJS = $(GCC_OBJS) $(OBJS) libbackend.o \
   @TREEBROWSER@ main.o gccspec.o version.o intl.o prefix.o cppspec.o \
   $(COLLECT2_OBJS) $(EXTRA_GCC_OBJS) mips-tfile.o mips-tdump.o \
   $(GCOV_OBJS) $(GCOV_DUMP_OBJS)
@@ -1798,9 +1801,9 @@  rest.cross: specs
 compilations: $(BACKEND)
 
 # This archive is strictly for the host.
-libbackend.a: $(OBJS)
+libbackend.a: $(OBJS@onestep@)
 	-rm -rf libbackend.a
-	$(AR) $(AR_FLAGS) libbackend.a $(OBJS)
+	$(AR) $(AR_FLAGS) libbackend.a $(OBJS@onestep@)
 	-$(RANLIB) $(RANLIB_FLAGS) libbackend.a
 
 # We call this executable `xgcc' rather than `gcc'
@@ -2243,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) \
@@ -3169,7 +3168,7 @@  df-core.o : df-core.c $(CONFIG_H) $(SYSTEM_H) core
 df-problems.o : df-problems.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) \
    $(RTL_H) insn-config.h $(RECOG_H) $(FUNCTION_H) $(REGS_H) alloc-pool.h \
    hard-reg-set.h $(BASIC_BLOCK_H) $(DF_H) $(BITMAP_H) sbitmap.h $(TIMEVAR_H) \
-   $(TM_P_H) $(TARGET_H) $(FLAGS_H) output.h $(EXCEPT_H) dce.h vecprim.h
+   $(TM_P_H) $(FLAGS_H) output.h $(EXCEPT_H) dce.h vecprim.h
 df-scan.o : df-scan.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) \
    insn-config.h $(RECOG_H) $(FUNCTION_H) $(REGS_H) alloc-pool.h \
    hard-reg-set.h $(BASIC_BLOCK_H) $(DF_H) $(BITMAP_H) sbitmap.h $(TIMEVAR_H) \
@@ -3523,6 +3522,19 @@  mips-tdump: mips-tdump.o version.o $(LIBDEPS)
 mips-tdump.o : mips-tdump.c $(CONFIG_H) $(RTL_H) $(SYSTEM_H) coretypes.h \
    $(TM_H) version.h $(srcdir)/../include/getopt.h stab.def
 
+# FIXME: writing proper dependencies for this is a *LOT* of work.
+libbackend.o : $(OBJS-common:.o=.c) $(out_file) \
+  insn-config.h insn-flags.h insn-codes.h insn-constants.h \
+  insn-attr.h  $(DATESTAMP) $(BASEVER) $(DEVPHASE) gcov-iov.h
+	$(COMPILER) $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) \
+	  -DTARGET_NAME=\"$(target_noncanonical)\" \
+	  -DLOCALEDIR=\"$(localedir)\" \
+	  -c $(filter %.c,$^) -o $@  \
+	  -DBASEVER=$(BASEVER_s) -DDATESTAMP=$(DATESTAMP_s) \
+	  -DREVISION=$(REVISION_s) \
+	  -DDEVPHASE=$(DEVPHASE_s) -DPKGVERSION=$(PKGVERSION_s) \
+	  -DBUGURL=$(BUGURL_s) -combine
+
 #
 # Generate header and source files from the machine description,
 # and compile them.
@@ -3794,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 \
@@ -3814,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
@@ -3925,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.