From 5f66ad3071b4085b1b2b0b263bcc7e4728b772ec Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Fri, 29 Apr 2016 17:35:59 -0400
Subject: [PATCH 23/35] FIXME: initial attempt at using diagnostics from gen*

---
 gcc/Makefile.in             | 84 +++++++++++++++++++++++++++++++--------------
 gcc/coretypes.h             |  2 +-
 gcc/diagnostic-color.c      |  7 ++++
 gcc/diagnostic-show-locus.c |  9 ++++-
 gcc/diagnostic.c            | 40 +++++++++++++++++----
 gcc/errors.c                | 73 ---------------------------------------
 gcc/errors.h                | 10 +++---
 gcc/genattrtab.c            |  4 +--
 gcc/genautomata.c           | 16 ++++-----
 gcc/gencfn-macros.c         |  2 +-
 gcc/gencheck.c              |  3 --
 gcc/genextract.c            |  7 ++--
 gcc/genflags.c              |  2 +-
 gcc/genmatch.c              | 18 ----------
 gcc/genmodes.c              |  2 +-
 gcc/genoutput.c             |  7 ++--
 gcc/genpreds.c              |  2 +-
 gcc/genrecog.c              |  6 ++--
 gcc/gensupport.c            | 11 +++---
 gcc/gentarget-def.c         |  2 +-
 gcc/ggc.h                   |  2 ++
 gcc/hash-map.h              |  2 ++
 gcc/input.c                 | 12 +++++--
 gcc/intl.c                  | 10 ++++++
 gcc/memory-block.cc         |  7 ++++
 gcc/pretty-print.c          |  7 ++++
 gcc/pretty-print.h          |  2 ++
 gcc/read-md.c               | 74 ++++++++-------------------------------
 gcc/read-md.h               |  8 ++---
 gcc/selftest.c              |  6 ++++
 gcc/vec.h                   |  2 ++
 31 files changed, 207 insertions(+), 232 deletions(-)

diff --git a/gcc/Makefile.in b/gcc/Makefile.in
index dd5ff06..6bea3ff 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -887,8 +887,8 @@ endif
 
 # Shorthand variables for dependency lists.
 DUMPFILE_H = $(srcdir)/../libcpp/include/line-map.h dumpfile.h
-VEC_H = vec.h statistics.h $(GGC_H)
-HASH_TABLE_H = $(HASHTAB_H) hash-table.h $(GGC_H)
+VEC_H = vec.h statistics.h
+HASH_TABLE_H = $(HASHTAB_H) hash-table.h
 EXCEPT_H = except.h $(HASHTAB_H)
 TARGET_DEF = target.def target-hooks-macros.h target-insns.def
 C_TARGET_DEF = c-family/c-target.def target-hooks-macros.h
@@ -1057,9 +1057,21 @@ GNATMAKE = @GNATMAKE@
 # Libs needed (at present) just for jcf-dump.
 LDEXP_LIB = @LDEXP_LIB@
 
+BUILD-OBJS-libcommon = build/diagnostic.o build/diagnostic-color.o \
+	build/diagnostic-show-locus.o build/pretty-print.o build/intl.o \
+	build/vec.o build/input.o build/version.o build/hash-table.o \
+	build/ggc-none.o build/memory-block.o build/selftest.o
+
+build/libcommon.a: $(BUILD-OBJS-libcommon)
+	-rm -rf build/libcommon.a
+	$(AR) $(AR_FLAGS) build/libcommon.a $(BUILD-OBJS-libcommon)
+	-$(RANLIB) $(RANLIB_FLAGS) build/libcommon.a
+
+BUILD_LIBCOMMON = build/libcommon.a
+
 # Likewise, for use in the tools that must run on this machine
 # even if we are cross-building GCC.
-BUILD_LIBS = $(BUILD_CPPLIB) $(BUILD_LIBIBERTY)
+BUILD_LIBS = $(BUILD_LIBCOMMON) $(BUILD_CPPLIB) $(BUILD_LIBIBERTY)
 
 BUILD_RTL = build/rtl.o build/read-rtl.o build/ggc-none.o \
 	    build/vec.o build/min-insn-modes.o build/gensupport.o \
@@ -2547,8 +2559,7 @@ build/errors.o : errors.c $(BCONFIG_H) $(SYSTEM_H) errors.h
 build/gensupport.o: gensupport.c $(BCONFIG_H) $(SYSTEM_H) coretypes.h	\
   $(GTM_H) $(RTL_BASE_H) $(OBSTACK_H) errors.h $(HASHTAB_H)		\
   $(READ_MD_H) $(GENSUPPORT_H)
-build/ggc-none.o : ggc-none.c $(BCONFIG_H) $(SYSTEM_H) coretypes.h 	\
-  $(GGC_H)
+build/ggc-none.o : ggc-none.c $(BCONFIG_H) $(SYSTEM_H) coretypes.h
 build/min-insn-modes.o : min-insn-modes.c $(BCONFIG_H) $(SYSTEM_H)	\
   $(MACHMODE_H)
 build/print-rtl.o: print-rtl.c $(BCONFIG_H) $(SYSTEM_H) coretypes.h	\
@@ -2559,11 +2570,11 @@ build/read-rtl.o: read-rtl.c $(BCONFIG_H) $(SYSTEM_H) coretypes.h	\
   $(GTM_H) $(RTL_BASE_H) $(OBSTACK_H) $(HASHTAB_H) $(READ_MD_H)		\
   $(GENSUPPORT_H)
 build/rtl.o: rtl.c $(BCONFIG_H) coretypes.h $(GTM_H) $(SYSTEM_H)	\
-  $(RTL_H) $(GGC_H) errors.h
+  $(RTL_H) errors.h
 build/vec.o : vec.c $(BCONFIG_H) $(SYSTEM_H) coretypes.h $(VEC_H)	\
-   $(GGC_H) toplev.h $(DIAGNOSTIC_CORE_H)
+  toplev.h $(DIAGNOSTIC_CORE_H)
 build/hash-table.o : hash-table.c $(BCONFIG_H) $(SYSTEM_H) coretypes.h  \
-   $(HASH_TABLE_H) $(GGC_H) toplev.h $(DIAGNOSTIC_CORE_H)
+   $(HASH_TABLE_H) toplev.h $(DIAGNOSTIC_CORE_H)
 build/inchash.o : inchash.c $(BCONFIG_H) $(SYSTEM_H) coretypes.h  \
    $(HASHTAB_H) inchash.h
 build/gencondmd.o : build/gencondmd.c $(BCONFIG_H) $(SYSTEM_H)		\
@@ -2577,13 +2588,34 @@ build/gencondmd.o : build/gencondmd.c $(BCONFIG_H) $(SYSTEM_H)		\
 build/gencondmd.o : \
   BUILD_CFLAGS := $(filter-out -fkeep-inline-functions, $(BUILD_CFLAGS))
 
+build/diagnostic-color.o :  diagnostic-color.c
+# FIXME
+
+build/diagnostic-show-locus.o :  diagnostic-show-locus.c
+# FIXME
+
+build/pretty-print.o :  pretty-print.c
+# FIXME
+
+build/intl.o :  intl.c
+# FIXME
+
+build/input.o :  input.c
+# FIXME
+
+build/memory-block.o :  memory-block.cc
+# FIXME
+
+build/selftest.o :  selftest.c
+# FIXME
+
 # ...these are the programs themselves.
 build/genattr.o : genattr.c $(RTL_BASE_H) $(BCONFIG_H) $(SYSTEM_H)	\
   coretypes.h $(GTM_H) errors.h $(READ_MD_H) $(GENSUPPORT_H)
 build/genattr-common.o : genattr-common.c $(RTL_BASE_H) $(BCONFIG_H)	\
   $(SYSTEM_H) coretypes.h $(GTM_H) errors.h $(READ_MD_H) $(GENSUPPORT_H)
 build/genattrtab.o : genattrtab.c $(RTL_BASE_H) $(OBSTACK_H)		\
-  $(BCONFIG_H) $(SYSTEM_H) coretypes.h $(GTM_H) errors.h $(GGC_H)	\
+  $(BCONFIG_H) $(SYSTEM_H) coretypes.h $(GTM_H) errors.h 		\
   $(READ_MD_H) $(GENSUPPORT_H) $(FNMATCH_H)
 build/genautomata.o : genautomata.c $(RTL_BASE_H) $(OBSTACK_H)		\
   $(BCONFIG_H) $(SYSTEM_H) coretypes.h $(GTM_H) errors.h $(VEC_H)	\
@@ -2648,32 +2680,32 @@ build/gengtype.o: $(BCONFIG_H)
 CFLAGS-errors.o += -DHOST_GENERATOR_FILE
 
 build/genmddeps.o: genmddeps.c $(BCONFIG_H) $(SYSTEM_H) coretypes.h	\
-  errors.h $(READ_MD_H) $(BUILD_CPPLIB)
+  errors.h $(READ_MD_H) $(BUILD_LIBS)
 build/genmodes.o : genmodes.c $(BCONFIG_H) $(SYSTEM_H) errors.h		\
-  $(HASHTAB_H) machmode.def $(extra_modes_file) $(BUILD_CPPLIB)
+  $(HASHTAB_H) machmode.def $(extra_modes_file) $(BUILD_LIBS)
 build/genopinit.o : genopinit.c $(RTL_BASE_H) $(BCONFIG_H) $(SYSTEM_H)	\
-  coretypes.h $(GTM_H) errors.h $(GENSUPPORT_H) optabs.def $(BUILD_CPPLIB)
+  coretypes.h $(GTM_H) errors.h $(GENSUPPORT_H) optabs.def $(BUILD_LIBS)
 build/genoutput.o : genoutput.c $(RTL_BASE_H) $(BCONFIG_H) $(SYSTEM_H)	\
-  coretypes.h $(GTM_H) errors.h $(READ_MD_H) $(GENSUPPORT_H) $(BUILD_CPPLIB)
+  coretypes.h $(GTM_H) errors.h $(READ_MD_H) $(GENSUPPORT_H) $(BUILD_LIBS)
 build/genpeep.o : genpeep.c $(RTL_BASE_H) $(BCONFIG_H) $(SYSTEM_H)	\
   coretypes.h $(GTM_H) errors.h $(GENSUPPORT_H) toplev.h $(DIAGNOSTIC_CORE_H) \
-  $(BUILD_CPPLIB)
+  $(BUILD_LIBS)
 build/genpreds.o : genpreds.c $(RTL_BASE_H) $(BCONFIG_H) $(SYSTEM_H)	\
   coretypes.h $(GTM_H) errors.h $(READ_MD_H) $(GENSUPPORT_H) $(OBSTACK_H) \
-  $(BUILD_CPPLIB)
+  $(BUILD_LIBS)
 build/genrecog.o : genrecog.c $(RTL_BASE_H) $(BCONFIG_H) $(SYSTEM_H)	\
   coretypes.h $(GTM_H) errors.h $(READ_MD_H) $(GENSUPPORT_H)		\
-  $(HASH_TABLE_H) inchash.h $(BUILD_CPPLIB)
+  $(HASH_TABLE_H) inchash.h $(BUILD_LIBS)
 build/genhooks.o : genhooks.c $(TARGET_DEF) $(C_TARGET_DEF)		\
-  $(COMMON_TARGET_DEF) $(BCONFIG_H) $(SYSTEM_H) errors.h $(BUILD_CPPLIB)
+  $(COMMON_TARGET_DEF) $(BCONFIG_H) $(SYSTEM_H) errors.h $(BUILD_LIBS)
 build/genmddump.o : genmddump.c $(RTL_BASE_H) $(BCONFIG_H) $(SYSTEM_H)	\
-  coretypes.h $(GTM_H) errors.h $(READ_MD_H) $(GENSUPPORT_H) $(BUILD_CPPLIB)
+  coretypes.h $(GTM_H) errors.h $(READ_MD_H) $(GENSUPPORT_H) $(BUILD_LIBS)
 build/genmatch.o : genmatch.c $(BCONFIG_H) $(SYSTEM_H) \
-  coretypes.h errors.h $(HASH_TABLE_H) hash-map.h $(GGC_H) is-a.h \
-  tree.def builtins.def internal-fn.def $(BUILD_CPPLIB)
+  coretypes.h errors.h $(HASH_TABLE_H) hash-map.h is-a.h \
+  tree.def builtins.def internal-fn.def $(BUILD_LIBS)
 build/gencfn-macros.o : gencfn-macros.c $(BCONFIG_H) $(SYSTEM_H)	\
   coretypes.h errors.h $(HASH_TABLE_H) hash-set.h builtins.def internal-fn.def \
-  $(BUILD_CPPLIB)
+  $(BUILD_LIBS)
 
 # Compile the programs that generate insn-* from the machine description.
 # They are compiled with $(COMPILER_FOR_BUILD), and associated libraries,
@@ -2713,7 +2745,7 @@ build/genmatch$(build_exeext): BUILD_LIBDEPS += $(LIBINTL_DEP) $(LIBICONV_DEP)
 build/genmatch$(build_exeext): BUILD_LIBS += $(LIBINTL) $(LIBICONV)
 endif
 
-build/genmatch$(build_exeext) : $(BUILD_CPPLIB) \
+build/genmatch$(build_exeext) : $(BUILD_LIBS) \
   $(BUILD_ERRORS) build/vec.o build/hash-table.o
 
 # These programs are not linked with the MD reader.
@@ -2721,14 +2753,14 @@ build/gengtype$(build_exeext) : build/gengtype-lex.o build/gengtype-parse.o \
               build/gengtype-state.o build/version.o build/errors.o
 
 gengtype$(exeext) : gengtype.o gengtype-lex.o gengtype-parse.o \
-              gengtype-state.o version.o errors.o $(LIBDEPS)
+              gengtype-state.o version.o errors.o $(LIBDEPS) $(BUILD_LIBS)
 	+$(LINKER) $(ALL_LINKERFLAGS) $(LDFLAGS) -o $@ \
 	    $(filter-out ($LIBDEPS), $^) $(LIBS)
 
 # Rule for the generator programs:
-$(genprog:%=build/gen%$(build_exeext)): build/gen%$(build_exeext): build/gen%.o $(BUILD_LIBDEPS)
+$(genprog:%=build/gen%$(build_exeext)): build/gen%$(build_exeext): build/gen%.o $(BUILD_LIBS)
 	+$(LINKER_FOR_BUILD) $(BUILD_LINKERFLAGS) $(BUILD_LDFLAGS) -o $@ \
-	    $(filter-out $(BUILD_LIBDEPS), $^) $(BUILD_LIBS)
+	    $(filter-out $(BUILD_LIBS), $^) $(BUILD_LIBS)
 
 # Generated source files for gengtype.  Prepend inclusion of
 # bconfig.h because AIX requires _LARGE_FILES to be defined before
@@ -2740,6 +2772,8 @@ gengtype-lex.c : gengtype-lex.l
 	  mv $@.tmp $@; \
 	}
 
+build/diagnostic.o: diagnostic.c
+
 #
 # Remake internationalization support.
 CFLAGS-intl.o += -DLOCALEDIR=\"$(localedir)\"
diff --git a/gcc/coretypes.h b/gcc/coretypes.h
index b3a91a6..6c1f806 100644
--- a/gcc/coretypes.h
+++ b/gcc/coretypes.h
@@ -396,9 +396,9 @@ typedef unsigned char uchar;
 #include "fixed-value.h"
 #include "hash-table.h"
 #include "hash-set.h"
+#endif /* GENERATOR_FILE && !USED_FOR_TARGET */
 #include "input.h"
 #include "is-a.h"
 #include "memory-block.h"
-#endif /* GENERATOR_FILE && !USED_FOR_TARGET */
 
 #endif /* coretypes.h */
diff --git a/gcc/diagnostic-color.c b/gcc/diagnostic-color.c
index f76c87b..9344703 100644
--- a/gcc/diagnostic-color.c
+++ b/gcc/diagnostic-color.c
@@ -16,7 +16,14 @@
    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
    02110-1301, USA.  */
 
+/* This file is compiled twice: once for the generator programs
+   once for the compiler.  */
+#ifdef GENERATOR_FILE
+#include "bconfig.h"
+#else
 #include "config.h"
+#endif
+
 #include "system.h"
 #include "diagnostic-color.h"
 
diff --git a/gcc/diagnostic-show-locus.c b/gcc/diagnostic-show-locus.c
index 49f7f11..d0edecb 100644
--- a/gcc/diagnostic-show-locus.c
+++ b/gcc/diagnostic-show-locus.c
@@ -18,16 +18,23 @@ You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
+/* This file is compiled twice: once for the generator programs
+   once for the compiler.  */
+#ifdef GENERATOR_FILE
+#include "bconfig.h"
+#else
 #include "config.h"
+#endif
+
 #include "system.h"
 #include "coretypes.h"
 #include "version.h"
 #include "demangle.h"
 #include "intl.h"
-#include "backtrace.h"
 #include "diagnostic.h"
 #include "diagnostic-color.h"
 #include "selftest.h"
+#include "vec.h"
 
 #ifdef HAVE_TERMIOS_H
 # include <termios.h>
diff --git a/gcc/diagnostic.c b/gcc/diagnostic.c
index 4638b5f..1a49edf 100644
--- a/gcc/diagnostic.c
+++ b/gcc/diagnostic.c
@@ -22,13 +22,22 @@ along with GCC; see the file COPYING3.  If not see
 /* This file implements the language independent aspect of diagnostic
    message module.  */
 
+/* This file is compiled twice: once for the generator programs
+   once for the compiler.  */
+#ifdef GENERATOR_FILE
+#include "bconfig.h"
+#else
 #include "config.h"
+#endif
+
 #include "system.h"
 #include "coretypes.h"
 #include "version.h"
 #include "demangle.h"
 #include "intl.h"
+#ifndef GENERATOR_FILE
 #include "backtrace.h"
+#endif
 #include "diagnostic.h"
 #include "diagnostic-color.h"
 #include "selftest.h"
@@ -338,6 +347,8 @@ diagnostic_build_prefix (diagnostic_context *context,
   return result;
 }
 
+#ifndef GENERATOR_FILE
+
 /* Functions at which to stop the backtrace print.  It's not
    particularly helpful to print the callers of these functions.  */
 
@@ -431,6 +442,25 @@ bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
 	   errnum == 0 ? "" : xstrerror (errnum));
 }
 
+#endif /* #ifndef GENERATOR_FILE */
+
+/* Attempt to print a backtrace.  Return true if anything was printed.  */
+
+static bool
+maybe_print_backtrace (void)
+{
+  int count = 0;
+#ifndef GENERATOR_FILE
+  struct backtrace_state *state;
+
+  state = backtrace_create_state (NULL, 0, bt_err_callback, NULL);
+  if (state != NULL)
+    backtrace_full (state, 2, bt_callback, bt_err_callback,
+		    (void *) &count);
+#endif /* ifndef GENERATOR_FILE */
+  return count > 0;
+}
+
 /* Take any action which is expected to happen after the diagnostic
    is written out.  This function does not always return.  */
 void
@@ -472,20 +502,16 @@ diagnostic_action_after_output (diagnostic_context *context,
     case DK_ICE:
     case DK_ICE_NOBT:
       {
-	struct backtrace_state *state = NULL;
+	bool printed_backtrace_p = false;
 	if (diag_kind == DK_ICE)
-	  state = backtrace_create_state (NULL, 0, bt_err_callback, NULL);
-	int count = 0;
-	if (state != NULL)
-	  backtrace_full (state, 2, bt_callback, bt_err_callback,
-			  (void *) &count);
+	  printed_backtrace_p = maybe_print_backtrace ();
 
 	if (context->abort_on_error)
 	  real_abort ();
 
 	fnotice (stderr, "Please submit a full bug report,\n"
 		 "with preprocessed source if appropriate.\n");
-	if (count > 0)
+	if (printed_backtrace_p)
 	  fnotice (stderr,
 		   ("Please include the complete backtrace "
 		    "with any bug report.\n"));
diff --git a/gcc/errors.c b/gcc/errors.c
index ba27319..c98bb34 100644
--- a/gcc/errors.c
+++ b/gcc/errors.c
@@ -30,14 +30,6 @@ along with GCC; see the file COPYING3.  If not see
 #include "system.h"
 #include "errors.h"
 
-/* Set this to argv[0] at the beginning of main.  */
-
-const char *progname;
-
-/* Starts out 0, set to 1 if error is called.  */
-
-int have_error = 0;
-
 /* Print a warning message - output produced, but there may be problems.  */
 
 void
@@ -53,23 +45,6 @@ warning (const char *format, ...)
 }
 
 
-/* Print an error message - we keep going but the output is unusable.  */
-
-void
-error (const char *format, ...)
-{
-  va_list ap;
-
-  va_start (ap, format);
-  fprintf (stderr, "%s: error: ", progname);
-  vfprintf (stderr, format, ap);
-  va_end (ap);
-  fputc ('\n', stderr);
-
-  have_error = 1;
-}
-
-
 /* Fatal error - terminate execution immediately.  Does not return.  */
 
 void
@@ -84,51 +59,3 @@ fatal (const char *format, ...)
   fputc ('\n', stderr);
   exit (FATAL_EXIT_CODE);
 }
-
-/* Similar, but say we got an internal error.  */
-
-void
-internal_error (const char *format, ...)
-{
-  va_list ap;
-
-  va_start (ap, format);
-  fprintf (stderr, "%s: Internal error: ", progname);
-  vfprintf (stderr, format, ap);
-  va_end (ap);
-  fputc ('\n', stderr);
-  exit (FATAL_EXIT_CODE);
-}
-
-/* Given a partial pathname as input, return another pathname that
-   shares no directory elements with the pathname of __FILE__.  This
-   is used by fancy_abort() to print `Internal compiler error in expr.c'
-   instead of `Internal compiler error in ../../GCC/gcc/expr.c'.  This
-   version is meant to be used for the gen* programs and therefor need not
-   handle subdirectories.  */
-
-const char *
-trim_filename (const char *name)
-{
-  static const char this_file[] = __FILE__;
-  const char *p = name, *q = this_file;
-
-  /* Skip any parts the two filenames have in common.  */
-  while (*p == *q && *p != 0 && *q != 0)
-    p++, q++;
-
-  /* Now go backwards until the previous directory separator.  */
-  while (p > name && !IS_DIR_SEPARATOR (p[-1]))
-    p--;
-
-  return p;
-}
-
-/* "Fancy" abort.  Reports where in the compiler someone gave up.
-   This file is used only by build programs, so we're not as polite as
-   the version in diagnostic.c.  */
-void
-fancy_abort (const char *file, int line, const char *func)
-{
-  internal_error ("abort in %s, at %s:%d", func, trim_filename (file), line);
-}
diff --git a/gcc/errors.h b/gcc/errors.h
index a6973f3..d022192 100644
--- a/gcc/errors.h
+++ b/gcc/errors.h
@@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
+/* FIXME: update this comment.  */
 /* warning, error, and fatal.  These definitions are suitable for use
    in the generator programs; eventually we would like to use them in
    cc1 too, but that's a longer term project.
@@ -28,13 +29,10 @@ along with GCC; see the file COPYING3.  If not see
 #ifndef GCC_ERRORS_H
 #define GCC_ERRORS_H
 
+#include "input.h"
+#include "diagnostic-core.h"
+
 extern void warning (const char *, ...) ATTRIBUTE_PRINTF_1 ATTRIBUTE_COLD;
-extern void error (const char *, ...) ATTRIBUTE_PRINTF_1 ATTRIBUTE_COLD;
 extern void fatal (const char *, ...) ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF_1 ATTRIBUTE_COLD;
-extern void internal_error (const char *, ...) ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF_1 ATTRIBUTE_COLD;
-extern const char *trim_filename (const char *);
-
-extern int have_error;
-extern const char *progname;
 
 #endif /* ! GCC_ERRORS_H */
diff --git a/gcc/genattrtab.c b/gcc/genattrtab.c
index e2f980d..805a4cd 100644
--- a/gcc/genattrtab.c
+++ b/gcc/genattrtab.c
@@ -5269,7 +5269,7 @@ main (int argc, const char **argv)
 	insn_index_number++;
     }
 
-  if (have_error)
+  if (seen_error ())
     return FATAL_EXIT_CODE;
 
   /* If we didn't have a DEFINE_ASM_ATTRIBUTES, make a null one.  */
@@ -5313,7 +5313,7 @@ main (int argc, const char **argv)
       attr->default_val->value
 	= check_attr_value (attr->loc, attr->default_val->value, attr);
 
-  if (have_error)
+  if (seen_error ())
     return FATAL_EXIT_CODE;
 
   for (i = 0; i < MAX_ATTRS_INDEX; i++)
diff --git a/gcc/genautomata.c b/gcc/genautomata.c
index 92c8b5c..7549dda 100644
--- a/gcc/genautomata.c
+++ b/gcc/genautomata.c
@@ -3097,7 +3097,7 @@ check_all_description (void)
   process_regexp_decls ();
   check_usage ();
   check_loops_in_regexps ();
-  if (!have_error)
+  if (!seen_error ())
     evaluate_max_reserv_cycles ();
 }
 
@@ -9448,17 +9448,17 @@ expand_automata (void)
     fprintf (stderr, "done\n");
   ticker_off (&check_time);
   generation_time = create_ticker ();
-  if (!have_error)
+  if (!seen_error ())
     {
       transform_insn_regexps ();
       check_unit_distributions_to_automata ();
     }
-  if (!have_error)
+  if (!seen_error ())
     {
       generate ();
       check_automata_insn_issues ();
     }
-  if (!have_error)
+  if (!seen_error ())
     {
       form_important_insn_automata_lists ();
     }
@@ -9562,7 +9562,7 @@ write_automata (void)
   finish_insn_decl_table ();
   finish_decl_table ();
   obstack_free (&irp, NULL);
-  if (have_error && output_description_file != NULL)
+  if (seen_error () && output_description_file != NULL)
     remove (output_description_file_name);
 }
 
@@ -9631,13 +9631,13 @@ main (int argc, const char **argv)
 	break;
       }
 
-  if (have_error)
+  if (seen_error ())
     return FATAL_EXIT_CODE;
 
   if (decls.length () > 0)
     {
       expand_automata ();
-      if (!have_error)
+      if (!seen_error ())
 	{
 	  puts ("/* Generated automatically by the program `genautomata'\n"
 		"   from the machine description file `md'.  */\n\n"
@@ -9678,6 +9678,6 @@ main (int argc, const char **argv)
     }
 
   fflush (stdout);
-  return (ferror (stdout) != 0 || have_error
+  return (ferror (stdout) != 0 || seen_error ()
 	  ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
 }
diff --git a/gcc/gencfn-macros.c b/gcc/gencfn-macros.c
index ed758e8..c3d4e08 100644
--- a/gcc/gencfn-macros.c
+++ b/gcc/gencfn-macros.c
@@ -212,7 +212,7 @@ main (int argc, char **argv)
 	}
     }
 
-  if (fflush (stdout) || fclose (stdout) || have_error)
+  if (fflush (stdout) || fclose (stdout) || seen_error ())
     return FATAL_EXIT_CODE;
   return SUCCESS_EXIT_CODE;
 }
diff --git a/gcc/gencheck.c b/gcc/gencheck.c
index 23be4a8..5673d18 100644
--- a/gcc/gencheck.c
+++ b/gcc/gencheck.c
@@ -17,9 +17,6 @@ You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
-/* We don't have insn-modes.h, but we include tm.h.  */
-#define BITS_PER_UNIT 8
-
 #include "bconfig.h"
 #include "system.h"
 #include "coretypes.h"
diff --git a/gcc/genextract.c b/gcc/genextract.c
index e6d5337..db18b68 100644
--- a/gcc/genextract.c
+++ b/gcc/genextract.c
@@ -177,10 +177,7 @@ VEC_safe_set_locstr (md_rtx_info *info, vec<locstr> *vp,
   if (ix < (*vp).length ())
     {
       if ((*vp)[ix])
-	{
-	  message_at (info->loc, "repeated operand number %d", ix);
-	  have_error = 1;
-	}
+	error_at (info->loc, "repeated operand number %d", ix);
       else
         (*vp)[ix] = str;
     }
@@ -421,7 +418,7 @@ main (int argc, const char **argv)
 	break;
     }
 
-  if (have_error)
+  if (seen_error ())
     return FATAL_EXIT_CODE;
 
   print_header ();
diff --git a/gcc/genflags.c b/gcc/genflags.c
index 8b37b15..da49af6 100644
--- a/gcc/genflags.c
+++ b/gcc/genflags.c
@@ -244,7 +244,7 @@ main (int argc, const char **argv)
 
   puts ("\n#endif /* GCC_INSN_FLAGS_H */");
 
-  if (have_error || ferror (stdout) || fflush (stdout) || fclose (stdout))
+  if (seen_error ()|| ferror (stdout) || fflush (stdout) || fclose (stdout))
     return FATAL_EXIT_CODE;
 
   return SUCCESS_EXIT_CODE;
diff --git a/gcc/genmatch.c b/gcc/genmatch.c
index 02e945a..0359534 100644
--- a/gcc/genmatch.c
+++ b/gcc/genmatch.c
@@ -50,24 +50,6 @@ unsigned verbose;
 
 /* libccp helpers.  */
 
-static struct line_maps *line_table;
-
-/* The rich_location class within libcpp requires a way to expand
-   source_location instances, and relies on the client code
-   providing a symbol named
-     linemap_client_expand_location_to_spelling_point
-   to do this.
-
-   This is the implementation for genmatch.  */
-
-expanded_location
-linemap_client_expand_location_to_spelling_point (source_location loc)
-{
-  const struct line_map_ordinary *map;
-  loc = linemap_resolve_location (line_table, loc, LRK_SPELLING_LOCATION, &map);
-  return linemap_expand_location (line_table, map, loc);
-}
-
 static bool
 #if GCC_VERSION >= 4001
 __attribute__((format (printf, 5, 0)))
diff --git a/gcc/genmodes.c b/gcc/genmodes.c
index 1170d4f..3a013e0 100644
--- a/gcc/genmodes.c
+++ b/gcc/genmodes.c
@@ -1816,7 +1816,7 @@ main (int argc, char **argv)
   create_modes ();
   complete_all_modes ();
 
-  if (have_error)
+  if (seen_error ())
     return FATAL_EXIT_CODE;
 
   calc_wider_mode ();
diff --git a/gcc/genoutput.c b/gcc/genoutput.c
index 3241115..d8d7daa 100644
--- a/gcc/genoutput.c
+++ b/gcc/genoutput.c
@@ -842,10 +842,7 @@ validate_optab_operands (struct data *d)
   if (strncmp (d->name, "cstore", 6) == 0
       && d->name[strlen (d->name) - 1] == '4'
       && d->operand[0].mode == VOIDmode)
-    {
-      message_at (d->loc, "missing mode for operand 0 of cstore");
-      have_error = 1;
-    }
+    error_at (d->loc, "missing mode for operand 0 of cstore");
 }
 
 /* Look at a define_insn just read.  Assign its code number.  Record
@@ -1035,7 +1032,7 @@ main (int argc, const char **argv)
   output_get_insn_name ();
 
   fflush (stdout);
-  return (ferror (stdout) != 0 || have_error
+  return (ferror (stdout) != 0 || seen_error ()
 	? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
 }
 
diff --git a/gcc/genpreds.c b/gcc/genpreds.c
index ac03103..5c49274 100644
--- a/gcc/genpreds.c
+++ b/gcc/genpreds.c
@@ -1662,7 +1662,7 @@ main (int argc, const char **argv)
   else
     write_insn_preds_c ();
 
-  if (have_error || ferror (stdout) || fflush (stdout) || fclose (stdout))
+  if (seen_error () || ferror (stdout) || fflush (stdout) || fclose (stdout))
     return FATAL_EXIT_CODE;
 
   return SUCCESS_EXIT_CODE;
diff --git a/gcc/genrecog.c b/gcc/genrecog.c
index 056798c..1f5b9f7 100644
--- a/gcc/genrecog.c
+++ b/gcc/genrecog.c
@@ -650,7 +650,7 @@ validate_pattern (rtx pattern, md_rtx_info *info, rtx set, int set_code)
 	    && ! (set
 		  && GET_CODE (set) == SET
 		  && GET_CODE (SET_SRC (set)) == CALL))
-	  message_at (info->loc, "warning: operand %d missing mode?",
+	  warning_at (info->loc, 0, "operand %d missing mode?",
 		      XINT (pattern, 0));
 	return;
       }
@@ -707,7 +707,7 @@ validate_pattern (rtx pattern, md_rtx_info *info, rtx set, int set_code)
 	  {
 	    const char *which;
 	    which = (dmode == VOIDmode ? "destination" : "source");
-	    message_at (info->loc, "warning: %s missing a mode?", which);
+	    warning_at (info->loc, 0, "%s missing a mode?", which);
 	  }
 
 	if (dest != SET_DEST (pattern))
@@ -5314,7 +5314,7 @@ main (int argc, const char **argv)
 	}
     }
 
-  if (have_error)
+  if (seen_error ())
     return FATAL_EXIT_CODE;
 
   puts ("\n\n");
diff --git a/gcc/gensupport.c b/gcc/gensupport.c
index 0ab981a..9fdf0ef 100644
--- a/gcc/gensupport.c
+++ b/gcc/gensupport.c
@@ -27,6 +27,7 @@
 #include "read-md.h"
 #include "gensupport.h"
 #include "vec.h"
+#include "diagnostic.h"
 
 #define MAX_OPERANDS 40
 
@@ -2182,7 +2183,7 @@ process_define_cond_exec (void)
   struct queue_elem *elem;
 
   identify_predicable_attribute ();
-  if (have_error)
+  if (seen_error ())
     return;
 
   for (elem = define_cond_exec_queue; elem ; elem = elem->next)
@@ -2409,7 +2410,7 @@ gen_mnemonic_attr (void)
   const char *str, *p;
   int i;
 
-  if (have_error)
+  if (seen_error ())
     return;
 
   /* Look for the DEFINE_ATTR for `mnemonic'.  */
@@ -2524,8 +2525,10 @@ init_rtx_reader_args_cb (int argc, const char **argv,
   split_sequence_num = 1;
   peephole2_sequence_num = 1;
 
-  line_maps *line_table = XCNEW (struct line_maps);
+  line_table = XCNEW (struct line_maps);
   generator_linemap_init (line_table);
+  diagnostic_initialize (global_dc, 0);
+
   gen_reader *reader = new gen_reader (line_table);
   reader->read_md_files (argc, argv, parse_opt);
 
@@ -2543,7 +2546,7 @@ init_rtx_reader_args_cb (int argc, const char **argv,
   if (define_attr_queue != NULL)
     gen_mnemonic_attr ();
 
-  if (have_error)
+  if (seen_error ())
     {
       delete reader;
       return NULL;
diff --git a/gcc/gentarget-def.c b/gcc/gentarget-def.c
index adbb564..9dc1ead 100644
--- a/gcc/gentarget-def.c
+++ b/gcc/gentarget-def.c
@@ -331,7 +331,7 @@ main (int argc, const char **argv)
 
   printf ("\n#endif /* GCC_INSN_TARGET_DEF_H */\n");
 
-  if (have_error || ferror (stdout) || fflush (stdout) || fclose (stdout))
+  if (seen_error ()|| ferror (stdout) || fflush (stdout) || fclose (stdout))
     return FATAL_EXIT_CODE;
 
   return SUCCESS_EXIT_CODE;
diff --git a/gcc/ggc.h b/gcc/ggc.h
index 32e0f41..dce6ba4 100644
--- a/gcc/ggc.h
+++ b/gcc/ggc.h
@@ -30,7 +30,9 @@ extern const char empty_string[];	/* empty string */
 /* Internal functions and data structures used by the GTY
    machinery, including the generated gt*.[hc] files.  */
 
+#if !defined (GENERATOR_FILE) && !defined (USED_FOR_TARGET)
 #include "gtype-desc.h"
+#endif
 
 /* One of these applies its third parameter (with cookie in the fourth
    parameter) to each pointer in the object pointed to by the first
diff --git a/gcc/hash-map.h b/gcc/hash-map.h
index 2d18494..ca39fce 100644
--- a/gcc/hash-map.h
+++ b/gcc/hash-map.h
@@ -117,6 +117,7 @@ public:
     : m_table (h.m_table, ggc, gather_mem_stats,
 	       HASH_MAP_ORIGIN PASS_MEM_STAT) {}
 
+#if !defined (GENERATOR_FILE) && !defined (USED_FOR_TARGET)
   /* Create a hash_map in ggc memory.  */
   static hash_map *create_ggc (size_t size,
 			       bool gather_mem_stats = GATHER_STATISTICS
@@ -126,6 +127,7 @@ public:
       new (map) hash_map (size, true, gather_mem_stats PASS_MEM_STAT);
       return map;
     }
+#endif /* #if !defined (GENERATOR_FILE) && !defined (USED_FOR_TARGET) */
 
   /* If key k isn't already in the map add key k with value v to the map, and
      return false.  Otherwise set the value of the entry for key k to be v and
diff --git a/gcc/input.c b/gcc/input.c
index dad640e..a8008fa 100644
--- a/gcc/input.c
+++ b/gcc/input.c
@@ -17,13 +17,21 @@ You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
+/* This file is compiled twice: once for the generator programs
+   once for the compiler.  */
+#ifdef GENERATOR_FILE
+#include "bconfig.h"
+#else
 #include "config.h"
+#endif
+
 #include "system.h"
 #include "coretypes.h"
 #include "intl.h"
 #include "diagnostic-core.h"
 #include "selftest.h"
 #include "cpplib.h"
+#include "vec.h"
 
 /* This is a cache used by get_next_line to store the content of a
    file to be searched for file lines.  */
@@ -1255,7 +1263,7 @@ dump_location_info (FILE *stream)
 				MAX_SOURCE_LOCATION + 1, UINT_MAX);
 }
 
-#if CHECKING_P
+#if CHECKING_P  && !defined (GENERATOR_FILE)
 
 namespace selftest {
 
@@ -1653,4 +1661,4 @@ input_c_tests ()
 
 } // namespace selftest
 
-#endif /* CHECKING_P */
+#endif /* #if CHECKING_P  && !defined (GENERATOR_FILE) */
diff --git a/gcc/intl.c b/gcc/intl.c
index 3e355d7..49a894b 100644
--- a/gcc/intl.c
+++ b/gcc/intl.c
@@ -17,7 +17,14 @@ You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
+/* This file is compiled twice: once for the generator programs
+   once for the compiler.  */
+#ifdef GENERATOR_FILE
+#include "bconfig.h"
+#else
 #include "config.h"
+#endif
+
 #include "system.h"
 #include "coretypes.h"
 #include "intl.h"
@@ -38,6 +45,8 @@ const char *locale_encoding = NULL;
 /* Whether the locale is using UTF-8.  */
 bool locale_utf8 = false;
 
+#ifndef GENERATOR_FILE
+
 #ifdef ENABLE_NLS
 
 /* Initialize the translation library for GCC.  This performs the
@@ -146,5 +155,6 @@ get_spaces (const char *str)
    return spaces;
 }
 
+#endif /* #ifndef GENERATOR_FILE */
 
 
diff --git a/gcc/memory-block.cc b/gcc/memory-block.cc
index cce30fa..7dff00d 100644
--- a/gcc/memory-block.cc
+++ b/gcc/memory-block.cc
@@ -17,7 +17,14 @@ You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
+/* This file is compiled twice: once for the generator programs
+   once for the compiler.  */
+#ifdef GENERATOR_FILE
+#include "bconfig.h"
+#else
 #include "config.h"
+#endif
+
 #include "system.h"
 #include "coretypes.h"
 #include "memory-block.h"
diff --git a/gcc/pretty-print.c b/gcc/pretty-print.c
index 325263e..53b1e19 100644
--- a/gcc/pretty-print.c
+++ b/gcc/pretty-print.c
@@ -18,7 +18,14 @@ You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
+/* This file is compiled twice: once for the generator programs
+   once for the compiler.  */
+#ifdef GENERATOR_FILE
+#include "bconfig.h"
+#else
 #include "config.h"
+#endif
+
 #include "system.h"
 #include "coretypes.h"
 #include "intl.h"
diff --git a/gcc/pretty-print.h b/gcc/pretty-print.h
index f49f35a..2837811 100644
--- a/gcc/pretty-print.h
+++ b/gcc/pretty-print.h
@@ -22,7 +22,9 @@ along with GCC; see the file COPYING3.  If not see
 #define GCC_PRETTY_PRINT_H
 
 #include "obstack.h"
+#if !defined (GENERATOR_FILE) && !defined (USED_FOR_TARGET)
 #include "wide-int-print.h"
+#endif
 
 /* Maximum number of format string arguments.  */
 #define PP_NL_ARGMAX   30
diff --git a/gcc/read-md.c b/gcc/read-md.c
index 9e44242..3fea435 100644
--- a/gcc/read-md.c
+++ b/gcc/read-md.c
@@ -17,14 +17,19 @@ You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
+/* This file is compiled twice: once for the generator programs
+   once for the compiler.  */
+#ifdef GENERATOR_FILE
 #include "bconfig.h"
+#else
+#include "config.h"
+#endif
+
 #include "system.h"
 #include "coretypes.h"
 #include "errors.h"
 #include "read-md.h"
-#ifndef GENERATOR_FILE
 #include "diagnostic.h"
-#endif
 
 /* Associates PTR (which can be a string, etc.) with the file location
    specified by FILENAME and LINENO.  */
@@ -217,68 +222,13 @@ print_c_condition (const char *cond)
   fprint_c_condition (stdout, cond);
 }
 
-/* A vfprintf-like function for reporting an error against line LINENO
-   of the current MD file.  */
-
-static void ATTRIBUTE_PRINTF(2,0)
-message_at_1 (file_location loc, const char *msg, va_list ap)
-{
-  expanded_location xloc
-    = linemap_client_expand_location_to_spelling_point (loc);
-  fprintf (stderr, "%s:%d: ", xloc.file, xloc.line);
-  vfprintf (stderr, msg, ap);
-  fputc ('\n', stderr);
-}
-
-/* A printf-like function for reporting a message against location LOC.  */
-
-void
-message_at (file_location loc, const char *msg, ...)
-{
-  va_list ap;
-
-  va_start (ap, msg);
-  message_at_1 (loc, msg, ap);
-  va_end (ap);
-}
-
-/* Like message_at, but treat the condition as an error.  */
-
-#ifdef GENERATOR_FILE
-
-void
-error_at (file_location loc, const char *msg, ...)
-{
-  va_list ap;
-
-  va_start (ap, msg);
-  message_at_1 (loc, msg, ap);
-  va_end (ap);
-  have_error = 1;
-}
-
-#endif /* #ifdef GENERATOR_FILE */
-
-/* Like message_at, but treat the condition as a fatal error.  */
-
-void
-fatal_at (file_location loc, const char *msg, ...)
-{
-  va_list ap;
-
-  va_start (ap, msg);
-  message_at_1 (loc, msg, ap);
-  va_end (ap);
-  exit (1);
-}
-
 /* A printf-like function for reporting an error against the current
    position in the MD file.  */
 
 void
 fatal_with_file_and_line (const char *msg, ...)
 {
-#ifdef GENERATOR_FILE
+#if 0 /* def GENERATOR_FILE */
   char context[64];
   size_t i;
   int c;
@@ -1224,6 +1174,10 @@ rtx_reader::update_location (bool advancing)
 			    1024 /* max_column_hint */);
       linemap_position_for_column (m_line_table, m_read_md_colno);
     }
+
+#if 0
+  input_location = get_current_location ();
+#endif  
 }
 
 /* Parse a -I option with argument ARG.  */
@@ -1357,7 +1311,7 @@ rtx_reader::read_md_files (int argc, const char **argv,
       handle_toplevel_file ();
     }
 
-  return !have_error;
+  return !seen_error ();
 }
 
 /* class noop_reader : public rtx_reader */
@@ -1369,7 +1323,7 @@ noop_reader::handle_unknown_directive (file_location loc, const char *)
   read_skip_construct (1, loc);
 }
 
-#ifdef GENERATOR_FILE
+#if 0//def GENERATOR_FILE
 
 expanded_location
 linemap_client_expand_location_to_spelling_point (source_location loc)
diff --git a/gcc/read-md.h b/gcc/read-md.h
index 1414524..893a65d 100644
--- a/gcc/read-md.h
+++ b/gcc/read-md.h
@@ -22,6 +22,7 @@ along with GCC; see the file COPYING3.  If not see
 
 #include "obstack.h"
 #include "line-map.h"
+#include "diagnostic-core.h"
 
 typedef source_location file_location;
 
@@ -237,11 +238,10 @@ extern void fprint_md_ptr_loc (FILE *, const void *);
 extern const char *join_c_conditions (const char *, const char *);
 extern void print_c_condition (const char *);
 extern void fprint_c_condition (FILE *, const char *);
-extern void message_at (file_location, const char *, ...) ATTRIBUTE_PRINTF_2;
-extern void error_at (file_location, const char *, ...) ATTRIBUTE_PRINTF_2;
-extern void fatal_at (file_location, const char *, ...) ATTRIBUTE_PRINTF_2;
+#define message_at inform
+#define fatal_at fatal_error
 extern void fatal_with_file_and_line (const char *, ...)
-  ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
+  ATTRIBUTE_GCC_DIAG(1,2) ATTRIBUTE_NORETURN;
 extern void fatal_expected_char (int, int) ATTRIBUTE_NORETURN;
 extern int read_skip_spaces (void);
 extern void require_char (char expected);
diff --git a/gcc/selftest.c b/gcc/selftest.c
index 2eeb6f2..2c25e8b 100644
--- a/gcc/selftest.c
+++ b/gcc/selftest.c
@@ -17,7 +17,13 @@ You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
+/* This file is compiled twice: once for the generator programs
+   once for the compiler.  */
+#ifdef GENERATOR_FILE
+#include "bconfig.h"
+#else
 #include "config.h"
+#endif
 #include "system.h"
 #include "coretypes.h"
 #include "selftest.h"
diff --git a/gcc/vec.h b/gcc/vec.h
index eb8c270..9c3f764 100644
--- a/gcc/vec.h
+++ b/gcc/vec.h
@@ -22,6 +22,8 @@ along with GCC; see the file COPYING3.  If not see
 #ifndef GCC_VEC_H
 #define GCC_VEC_H
 
+#include "statistics.h"
+
 /* Some gen* file have no ggc support as the header file gtype-desc.h is
    missing.  Provide these definitions in case ggc.h has not been included.
    This is not a problem because any code that runs before gengtype is built
-- 
1.8.5.3

