From 0bc9f762e114ac3cf51c4185cab5e3ec40dfdd6e Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Wed, 6 Apr 2016 15:54:10 -0400
Subject: [PATCH 18/39] FIXME: use libcpp for location-handling

---
 gcc/Makefile.in        | 27 +++++++++--------
 gcc/genattrtab.c       | 13 +++++----
 gcc/genconstants.c     |  4 ++-
 gcc/genemit.c          | 28 +++++++++++++-----
 gcc/genenums.c         |  4 ++-
 gcc/genmddeps.c        |  4 ++-
 gcc/genoutput.c        |  6 ++--
 gcc/gensupport.c       |  7 +++--
 gcc/read-md.c          | 78 ++++++++++++++++++++++++++++++++++++++++++++++++--
 gcc/read-md.h          | 29 ++++++++++++-------
 gcc/rtl/rtl-frontend.c |  3 +-
 11 files changed, 158 insertions(+), 45 deletions(-)

diff --git a/gcc/Makefile.in b/gcc/Makefile.in
index 3d6f0e6..e6a17d8 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -1047,7 +1047,7 @@ LDEXP_LIB = @LDEXP_LIB@
 
 # Likewise, for use in the tools that must run on this machine
 # even if we are cross-building GCC.
-BUILD_LIBS = $(BUILD_LIBIBERTY)
+BUILD_LIBS = $(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 \
@@ -2589,29 +2589,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)
+  errors.h $(READ_MD_H) $(BUILD_CPPLIB)
 build/genmodes.o : genmodes.c $(BCONFIG_H) $(SYSTEM_H) errors.h		\
-  $(HASHTAB_H) machmode.def $(extra_modes_file)
+  $(HASHTAB_H) machmode.def $(extra_modes_file) $(BUILD_CPPLIB)
 build/genopinit.o : genopinit.c $(RTL_BASE_H) $(BCONFIG_H) $(SYSTEM_H)	\
-  coretypes.h $(GTM_H) errors.h $(GENSUPPORT_H) optabs.def
+  coretypes.h $(GTM_H) errors.h $(GENSUPPORT_H) optabs.def $(BUILD_CPPLIB)
 build/genoutput.o : genoutput.c $(RTL_BASE_H) $(BCONFIG_H) $(SYSTEM_H)	\
-  coretypes.h $(GTM_H) errors.h $(READ_MD_H) $(GENSUPPORT_H)
+  coretypes.h $(GTM_H) errors.h $(READ_MD_H) $(GENSUPPORT_H) $(BUILD_CPPLIB)
 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)
+  coretypes.h $(GTM_H) errors.h $(GENSUPPORT_H) toplev.h $(DIAGNOSTIC_CORE_H) \
+  $(BUILD_CPPLIB)
 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)
+  coretypes.h $(GTM_H) errors.h $(READ_MD_H) $(GENSUPPORT_H) $(OBSTACK_H) \
+  $(BUILD_CPPLIB)
 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
+  $(HASH_TABLE_H) inchash.h $(BUILD_CPPLIB)
 build/genhooks.o : genhooks.c $(TARGET_DEF) $(C_TARGET_DEF)		\
-  $(COMMON_TARGET_DEF) $(BCONFIG_H) $(SYSTEM_H) errors.h
+  $(COMMON_TARGET_DEF) $(BCONFIG_H) $(SYSTEM_H) errors.h $(BUILD_CPPLIB)
 build/genmddump.o : genmddump.c $(RTL_BASE_H) $(BCONFIG_H) $(SYSTEM_H)	\
-  coretypes.h $(GTM_H) errors.h $(READ_MD_H) $(GENSUPPORT_H)
+  coretypes.h $(GTM_H) errors.h $(READ_MD_H) $(GENSUPPORT_H) $(BUILD_CPPLIB)
 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
+  tree.def builtins.def internal-fn.def $(BUILD_CPPLIB)
 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
+  coretypes.h errors.h $(HASH_TABLE_H) hash-set.h builtins.def internal-fn.def \
+  $(BUILD_CPPLIB)
 
 # Compile the programs that generate insn-* from the machine description.
 # They are compiled with $(COMPILER_FOR_BUILD), and associated libraries,
diff --git a/gcc/genattrtab.c b/gcc/genattrtab.c
index 91d04d4..7bed462 100644
--- a/gcc/genattrtab.c
+++ b/gcc/genattrtab.c
@@ -4235,9 +4235,12 @@ write_insn_cases (FILE *outf, struct insn_ent *ie, int indent)
       {
 	write_indent (outf, indent);
 	if (GET_CODE (ie->def->def) == DEFINE_PEEPHOLE)
-	  fprintf (outf, "case %d:  /* define_peephole, %s:%d */\n",
-		   ie->def->insn_code, ie->def->loc.filename,
-		   ie->def->loc.lineno);
+	  {
+	    expanded_location xloc
+	      = linemap_client_expand_location_to_spelling_point (ie->def->loc);
+	    fprintf (outf, "case %d:  /* define_peephole, %s:%d */\n",
+		     ie->def->insn_code, xloc.file, xloc.line);
+	  }
 	else
 	  fprintf (outf, "case %d:  /* %s */\n",
 		   ie->def->insn_code, XSTR (ie->def->def, 0));
@@ -4638,7 +4641,7 @@ make_internal_attr (const char *name, rtx value, int special)
   attr->is_numeric = 1;
   attr->is_const = 0;
   attr->is_special = (special & ATTR_SPECIAL) != 0;
-  attr->default_val = get_attr_value (file_location ("<internal>", 0),
+  attr->default_val = get_attr_value (GENERATOR_BUILTINS_LOCATION,
 				      value, attr, -2);
 }
 
@@ -5260,7 +5263,7 @@ main (int argc, const char **argv)
       md_rtx_info info;
       info.def = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
       XVEC (info.def, 0) = rtvec_alloc (0);
-      info.loc = file_location ("<internal>", 0);
+      info.loc = GENERATOR_BUILTINS_LOCATION;
       info.index = -1;
       gen_insn (&info);
     }
diff --git a/gcc/genconstants.c b/gcc/genconstants.c
index e8be5b6..c785d31 100644
--- a/gcc/genconstants.c
+++ b/gcc/genconstants.c
@@ -79,7 +79,9 @@ main (int argc, const char **argv)
 {
   progname = "genconstants";
 
-  noop_reader reader;
+  line_maps line_table;
+  generator_linemap_init (&line_table);
+  noop_reader reader (&line_table);
   if (!reader.read_md_files (argc, argv, NULL))
     return (FATAL_EXIT_CODE);
 
diff --git a/gcc/genemit.c b/gcc/genemit.c
index 33040aa..d28e2e2 100644
--- a/gcc/genemit.c
+++ b/gcc/genemit.c
@@ -397,7 +397,9 @@ gen_insn (md_rtx_info *info)
   if (XSTR (insn, 0)[0] == 0 || XSTR (insn, 0)[0] == '*')
     return;
 
-  printf ("/* %s:%d */\n", info->loc.filename, info->loc.lineno);
+  expanded_location xloc
+    = linemap_client_expand_location_to_spelling_point (info->loc);
+  printf ("/* %s:%d */\n", xloc.file, xloc.line);
 
   /* Find out how many operands this function has.  */
   get_pattern_stats (&stats, XVEC (insn, 1));
@@ -808,18 +810,30 @@ from the machine description file `md'.  */\n\n");
 	break;
 
       case DEFINE_EXPAND:
-	printf ("/* %s:%d */\n", info.loc.filename, info.loc.lineno);
-	gen_expand (&info);
+	{
+	  expanded_location xloc
+	    = linemap_client_expand_location_to_spelling_point (info.loc);
+	  printf ("/* %s:%d */\n", xloc.file, xloc.line);
+	  gen_expand (&info);
+	}
 	break;
 
       case DEFINE_SPLIT:
-	printf ("/* %s:%d */\n", info.loc.filename, info.loc.lineno);
-	gen_split (&info);
+	{
+	  expanded_location xloc
+	    = linemap_client_expand_location_to_spelling_point (info.loc);
+	  printf ("/* %s:%d */\n", xloc.file, xloc.line);
+	  gen_split (&info);
+	}
 	break;
 
       case DEFINE_PEEPHOLE2:
-	printf ("/* %s:%d */\n", info.loc.filename, info.loc.lineno);
-	gen_split (&info);
+	{
+	  expanded_location xloc
+	    = linemap_client_expand_location_to_spelling_point (info.loc);
+	  printf ("/* %s:%d */\n", xloc.file, xloc.line);
+	  gen_split (&info);
+	}
 	break;
 
       default:
diff --git a/gcc/genenums.c b/gcc/genenums.c
index 8af8d9a..22f8110 100644
--- a/gcc/genenums.c
+++ b/gcc/genenums.c
@@ -49,7 +49,9 @@ main (int argc, const char **argv)
 {
   progname = "genenums";
 
-  noop_reader reader;
+  line_maps line_table;
+  generator_linemap_init (&line_table);
+  noop_reader reader (&line_table);
   if (!reader.read_md_files (argc, argv, NULL))
     return (FATAL_EXIT_CODE);
 
diff --git a/gcc/genmddeps.c b/gcc/genmddeps.c
index e3d229d..9b5eafc 100644
--- a/gcc/genmddeps.c
+++ b/gcc/genmddeps.c
@@ -47,7 +47,9 @@ main (int argc, const char **argv)
   progname = "genmddeps";
   include_callback = add_filedep;
 
-  noop_reader reader;
+  line_maps line_table;
+  generator_linemap_init (&line_table);
+  noop_reader reader (&line_table);
   if (!reader.read_md_files (argc, argv, NULL))
     return FATAL_EXIT_CODE;
 
diff --git a/gcc/genoutput.c b/gcc/genoutput.c
index f8c25ac..3241115 100644
--- a/gcc/genoutput.c
+++ b/gcc/genoutput.c
@@ -292,7 +292,9 @@ output_insn_data (void)
 
   for (d = idata; d; d = d->next)
     {
-      printf ("  /* %s:%d */\n", d->loc.filename, d->loc.lineno);
+      expanded_location xloc
+	= linemap_client_expand_location_to_spelling_point (d->loc);
+      printf ("  /* %s:%d */\n", xloc.file, xloc.line);
       printf ("  {\n");
 
       if (d->name)
@@ -979,7 +981,7 @@ init_insn_for_nothing (void)
   idata = XCNEW (struct data);
   new (idata) data ();
   idata->name = "*placeholder_for_nothing";
-  idata->loc = file_location ("<internal>", 0);
+  idata->loc = GENERATOR_BUILTINS_LOCATION;
   idata_end = &idata->next;
 }
 
diff --git a/gcc/gensupport.c b/gcc/gensupport.c
index 901cc3d..b68010e 100644
--- a/gcc/gensupport.c
+++ b/gcc/gensupport.c
@@ -446,7 +446,7 @@ add_define_attr (const char *name)
   XEXP (t1, 2) = rtx_alloc (CONST_STRING);
   XSTR (XEXP (t1, 2), 0) = "yes";
   e->data = t1;
-  e->loc = file_location ("built-in", -1);
+  e->loc = GENERATOR_BUILTINS_LOCATION;
   e->next = define_attr_queue;
   define_attr_queue = e;
 
@@ -2231,6 +2231,7 @@ process_define_subst (void)
 class gen_reader : public rtx_reader
 {
  public:
+  gen_reader (line_maps *line_table) : rtx_reader (line_table) {}
   void handle_unknown_directive (file_location, const char *);
 };
 
@@ -2522,7 +2523,9 @@ init_rtx_reader_args_cb (int argc, const char **argv,
   split_sequence_num = 1;
   peephole2_sequence_num = 1;
 
-  gen_reader reader;
+  line_maps *line_table = XCNEW (struct line_maps);
+  generator_linemap_init (line_table);
+  gen_reader reader (line_table);
   reader.read_md_files (argc, argv, parse_opt);
 
   if (define_attr_queue != NULL)
diff --git a/gcc/read-md.c b/gcc/read-md.c
index ee6a67a..5983b2b 100644
--- a/gcc/read-md.c
+++ b/gcc/read-md.c
@@ -239,13 +239,17 @@ print_c_condition (const char *cond)
   fprint_c_condition (stdout, cond);
 }
 
+#ifdef GENERATOR_FILE
+
 /* 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)
 {
-  fprintf (stderr, "%s:%d: ", loc.filename, loc.lineno);
+  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);
 }
@@ -287,6 +291,7 @@ fatal_at (file_location loc, const char *msg, ...)
   va_end (ap);
   exit (1);
 }
+#endif
 
 /* A printf-like function for reporting an error against the current
    position in the MD file.  */
@@ -898,6 +903,29 @@ traverse_enum_types (htab_trav callback, void *info)
   htab_traverse (enum_types, callback, info);
 }
 
+
+/* FIXME.  */
+
+static line_maps *rtx_reader_line_table;
+
+/* FIXME.  */
+
+rtx_reader::rtx_reader (line_maps *line_table)
+: m_line_table (line_table),
+  m_current_linemap (NULL)
+{
+  rtx_reader_line_table = m_line_table;
+}
+
+/* FIXME.  */
+
+rtx_reader::~rtx_reader ()
+{
+#if 0
+  rtx_reader_line_table = NULL;
+#endif
+}
+
 /* Process an "include" directive, starting with the optional space
    after the "include".  Read in the file and use HANDLE_DIRECTIVE
    to process each unknown directive.  LINENO is the line number on
@@ -963,12 +991,14 @@ rtx_reader::handle_include (file_location loc)
 
   read_md_file = input_file;
   read_md_filename = pathname;
+  /* FIXME: update line_maps */
   handle_file ();
 
   /* Restore the old cursor.  */
   read_md_file = old_file;
   read_md_filename = old_filename;
   read_md_lineno = old_lineno;
+  /* FIXME: update line_maps */
 
   /* Do not free the pathname.  It is attached to the various rtx
      queue elements.  */
@@ -987,7 +1017,7 @@ rtx_reader::handle_file ()
   read_md_lineno = 1;
   while ((c = read_skip_spaces ()) != EOF)
     {
-      file_location loc (read_md_filename, read_md_lineno);
+      file_location loc = get_currrent_location ();
       if (c != '(')
 	fatal_expected_char ('(', c);
 
@@ -1025,9 +1055,21 @@ rtx_reader::handle_toplevel_file ()
   else
     base_dir = xstrndup (in_fname, base - in_fname);
 
+  m_current_linemap
+    = linemap_check_ordinary (linemap_add (m_line_table, LC_ENTER, 0,
+					   in_fname, 1));
+
   handle_file ();
 }
 
+file_location
+rtx_reader::get_currrent_location () const
+{
+  return linemap_position_for_line_and_column (m_line_table,
+					       m_current_linemap,
+					       read_md_lineno, 0);
+}
+
 /* Parse a -I option with argument ARG.  */
 
 static void
@@ -1172,3 +1214,35 @@ noop_reader::handle_unknown_directive (file_location loc, const char *)
 {
   read_skip_construct (1, loc);
 }
+
+#ifdef GENERATOR_FILE
+
+expanded_location
+linemap_client_expand_location_to_spelling_point (source_location loc)
+{
+  const struct line_map_ordinary *map;
+  loc = linemap_resolve_location (rtx_reader_line_table, loc,
+				  LRK_SPELLING_LOCATION, &map);
+  return linemap_expand_location (rtx_reader_line_table, map, loc);
+}
+
+#endif
+
+/* Helper for the linemap code.  */
+
+static size_t
+round_alloc_size (size_t s)
+{
+  return s;
+}
+
+/* FIXME.  */
+
+void
+generator_linemap_init (line_maps *line_table)
+{
+  linemap_init (line_table, 1);
+  line_table->reallocator = xrealloc;
+  line_table->round_alloc_size = round_alloc_size;
+  line_table->default_range_bits = 0;
+}
diff --git a/gcc/read-md.h b/gcc/read-md.h
index a9391e9..b9a604d 100644
--- a/gcc/read-md.h
+++ b/gcc/read-md.h
@@ -21,18 +21,9 @@ along with GCC; see the file COPYING3.  If not see
 #define GCC_READ_MD_H
 
 #include "obstack.h"
+#include "line-map.h"
 
-/* Records a position in the file.  */
-struct file_location {
-  file_location () {}
-  file_location (const char *, int);
-
-  const char *filename;
-  int lineno;
-};
-
-inline file_location::file_location (const char *filename_in, int lineno_in)
-  : filename (filename_in), lineno (lineno_in) {}
+typedef source_location file_location;
 
 /* Holds one symbol or number in the .md file.  */
 struct md_name {
@@ -93,6 +84,9 @@ struct enum_type {
 class rtx_reader
 {
  public:
+  rtx_reader (line_maps *line_table);
+  ~rtx_reader ();
+
   bool read_md_files (int, const char **, bool (*) (const char *));
 
   /* A hook that handles a single .md-file directive, up to but not
@@ -105,11 +99,19 @@ class rtx_reader
   void handle_file ();
   void handle_toplevel_file ();
   void handle_include (file_location loc);
+
+  file_location get_currrent_location () const;
+
+ private:
+  line_maps *m_line_table;
+  const line_map_ordinary *m_current_linemap;
 };
 
 class noop_reader : public rtx_reader
 {
  public:
+  noop_reader (line_maps *line_table) : rtx_reader (line_table) {}
+
   /* A dummy implementation which skips unknown directives.  */
   void handle_unknown_directive (file_location, const char *);
 };
@@ -152,8 +154,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 *);
+#ifdef GENERATOR_FILE
 extern void message_at (file_location, const char *, ...) ATTRIBUTE_PRINTF_2;
 extern void error_at (file_location, const char *, ...) ATTRIBUTE_PRINTF_2;
+#endif
 extern void fatal_at (file_location, const char *, ...) ATTRIBUTE_PRINTF_2;
 extern void fatal_with_file_and_line (const char *, ...)
   ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
@@ -169,4 +173,7 @@ extern void traverse_md_constants (htab_trav, void *);
 extern void traverse_enum_types (htab_trav, void *);
 extern struct enum_type *lookup_enum_type (const char *);
 
+#define GENERATOR_BUILTINS_LOCATION ((file_location) 1)
+extern void generator_linemap_init (line_maps *line_table);
+
 #endif /* GCC_READ_MD_H */
diff --git a/gcc/rtl/rtl-frontend.c b/gcc/rtl/rtl-frontend.c
index 9bc0438..ea2abf6 100644
--- a/gcc/rtl/rtl-frontend.c
+++ b/gcc/rtl/rtl-frontend.c
@@ -162,6 +162,7 @@ static hash_map<uid_hash, rtx> insns_by_uid;
 class function_reader : public rtx_reader
 {
  public:
+  function_reader (line_maps *set) : rtx_reader (set) {}
   void handle_unknown_directive (file_location, const char *);
 };
 
@@ -192,7 +193,7 @@ rtl_langhook_parse_file (void)
   argv.safe_push (progname);
   for (unsigned i = 0; i < num_in_fnames; i++)
     argv.safe_push (in_fnames[i]);
-  function_reader reader;
+  function_reader reader (line_table);
   if (reader.read_md_files (argv.length (), argv.address (), NULL))
     {
     }
-- 
1.8.5.3

