diff mbox

Ping! gengtype plugin improvement last2round - patch 1 [declprog]

Message ID 20101004214723.5c5b72a2.basile@starynkevitch.net
State New
Headers show

Commit Message

Basile Starynkevitch Oct. 4, 2010, 7:47 p.m. UTC
I am pinging once again
http://gcc.gnu.org/ml/gcc-patches/2010-09/msg01964.html taking into
account http://gcc.gnu.org/ml/gcc-patches/2010-09/msg02086.html

Attached patch is against trunk r164957. The changes w.r.t.
http://gcc.gnu.org/ml/gcc-patches/2010-09/msg01964.html are purely
cosmetic, to please Laurynas, by taking his remarks into account. If
the reviewer prefer the patch from
http://gcc.gnu.org/ml/gcc-patches/2010-09/msg01964.html please tell


The gcc/ChangeLog entry is the same as in
http://gcc.gnu.org/ml/gcc-patches/2010-09/msg01964.html:


#### gcc/ChangeLog entry
2010-10-04  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.

PS. I am beginning to lose hope getting this patch series into 4.6! I
can't understand who is interested in OK-ing them (Laurynas' status
don't permit it, he is a plain write after approval maintainer like me,
not a reviewer). In case some reviewer want to meet me on sunday
october 24th before the GCC Summit, I am arriving at Ottawa on saturday
evening.

Comments

Basile Starynkevitch Oct. 10, 2010, 1:08 p.m. UTC | #1
I am pinging again

http://gcc.gnu.org/ml/gcc-patches/2010-10/msg00227.html

Cheers.
Basile Starynkevitch Oct. 13, 2010, 4:48 p.m. UTC | #2
On Sun, 10 Oct 2010 15:08:20 +0200
Basile Starynkevitch <basile@starynkevitch.net> wrote:

> I am pinging again
> 
> http://gcc.gnu.org/ml/gcc-patches/2010-10/msg00227.html

And I am pinging it again again.

Cheers.
Diego Novillo Oct. 13, 2010, 4:50 p.m. UTC | #3
On Wed, Oct 13, 2010 at 16:48, Basile Starynkevitch
<basile@starynkevitch.net> wrote:
> On Sun, 10 Oct 2010 15:08:20 +0200
> Basile Starynkevitch <basile@starynkevitch.net> wrote:
>
>> I am pinging again
>>
>> http://gcc.gnu.org/ml/gcc-patches/2010-10/msg00227.html
>
> And I am pinging it again again.

I'll review it later today.  Apologies for the delay.


Diego.
Diego Novillo Oct. 14, 2010, 1:20 p.m. UTC | #4
On 10/04/10 15:47, Basile Starynkevitch wrote:

> PS. I am beginning to lose hope getting this patch series into 4.6! I
> can't understand who is interested in OK-ing them (Laurynas' status
> don't permit it, he is a plain write after approval maintainer like me,
> not a reviewer). In case some reviewer want to meet me on sunday
> october 24th before the GCC Summit, I am arriving at Ottawa on saturday
> evening.

Let's meet at the summit.  I will try to review this series of patches 
before stage 1 closes.  I have done a cursory review of this series of 
patches and I believe they are mostly OK to go in.

You've had the misfortune of venturing into no-man's land.  There are no 
reviewers/maintainers for this area of the compiler (I'm trying to 
address this atm).


> 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__)

Space before '('.

> #define create_optional_field(next,type,name,cond) \
> - create_optional_field_(next,type,name,cond,__LINE__)
> + create_optional_field_(next,type,name,cond,__LINE__)

Space before '('.


OK with those changes.  Please be on the lookout for build failures due 
to this.


Diego.
Basile Starynkevitch Oct. 14, 2010, 1:58 p.m. UTC | #5
On Thu, Oct 14, 2010 at 09:20:24AM -0400, Diego Novillo wrote:
> On 10/04/10 15:47, Basile Starynkevitch wrote:
> 
> >PS. I am beginning to lose hope getting this patch series into 4.6! I
> >can't understand who is interested in OK-ing them (Laurynas' status
> >don't permit it, he is a plain write after approval maintainer like me,
> >not a reviewer). In case some reviewer want to meet me on sunday
> >october 24th before the GCC Summit, I am arriving at Ottawa on saturday
> >evening.
> 
> Let's meet at the summit.  I will try to review this series of
> patches before stage 1 closes.  I have done a cursory review of this
> series of patches and I believe they are mostly OK to go in.

Diego, we can even meet on Sunday 24th october in Ottawa if you like.

> 
> You've had the misfortune of venturing into no-man's land.  There
> are no reviewers/maintainers for this area of the compiler (I'm
> trying to address this atm).
> 
> 
> >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__)
> 
> Space before '('.
> 
> >#define create_optional_field(next,type,name,cond) \
> >- create_optional_field_(next,type,name,cond,__LINE__)
> >+ create_optional_field_(next,type,name,cond,__LINE__)
> 
> Space before '('.
> 
> 
> OK with those changes.  Please be on the lookout for build failures
> due to this.

Committed as revision 165470 to the trunk, with the requested space changes.

A very big thanks to Diego Novillo for having taken time to review
that, and my congratulations to Jeremie Salvucci (in CC). He worked
hard on this series of patches, much more than we expected (his
internship was supposed to be mostly on MELT, and actually he spent
about the same time on gengtype patches and on GCC MELT).

Cheers.
diff mbox

Patch

Index: gcc/gengtype.c
===================================================================
--- gcc/gengtype.c	(revision 164957)
+++ 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,9 +252,13 @@  static lang_bitmap
 get_lang_bitmap (const char *gtfile)
 {
 
-  if (gtfile == this_file)
-    /* Things defined in this file are universal.  */
-    return (((lang_bitmap) 1) << num_lang_dirs) - 1;
+  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
     {
       lang_bitmap n = 0;
@@ -280,6 +282,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 +925,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 +954,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 +3291,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 +3793,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 +4284,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 +4553,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 164957)
+++ 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 164957)
+++ 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) \
@@ -3802,7 +3799,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 \
@@ -3822,6 +3819,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
@@ -3931,7 +3938,8 @@  genprog = $(genprogerr) check checksum condmd
 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
 
 # Rule for the generator programs:
 $(genprog:%=build/gen%$(build_exeext)): build/gen%$(build_exeext): build/gen%.o $(BUILD_LIBDEPS)