From 1f2ceed1e4e0a15a015cb82a0770b7cea2011d82 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Tue, 5 Apr 2016 15:07:32 -0400
Subject: [PATCH 017/117] FIXME: move more rtx parsing into rtx_reader

---
 gcc/genconstants.c     |  3 ++-
 gcc/genenums.c         |  3 ++-
 gcc/genmddeps.c        |  3 ++-
 gcc/gensupport.c       |  6 +++---
 gcc/read-md.c          | 50 +++++++++++++++++++++++++-------------------------
 gcc/read-md.h          | 18 +++++++++++++++---
 gcc/rtl/rtl-frontend.c |  7 +++----
 7 files changed, 52 insertions(+), 38 deletions(-)

diff --git a/gcc/genconstants.c b/gcc/genconstants.c
index c10e3e3..e8be5b6 100644
--- a/gcc/genconstants.c
+++ b/gcc/genconstants.c
@@ -79,7 +79,8 @@ main (int argc, const char **argv)
 {
   progname = "genconstants";
 
-  if (!read_md_files (argc, argv, NULL, NULL))
+  noop_reader reader;
+  if (!reader.read_md_files (argc, argv, NULL))
     return (FATAL_EXIT_CODE);
 
   /* Initializing the MD reader has the side effect of loading up
diff --git a/gcc/genenums.c b/gcc/genenums.c
index db46a67..8af8d9a 100644
--- a/gcc/genenums.c
+++ b/gcc/genenums.c
@@ -49,7 +49,8 @@ main (int argc, const char **argv)
 {
   progname = "genenums";
 
-  if (!read_md_files (argc, argv, NULL, NULL))
+  noop_reader reader;
+  if (!reader.read_md_files (argc, argv, NULL))
     return (FATAL_EXIT_CODE);
 
   puts ("/* Generated automatically by the program `genenums'");
diff --git a/gcc/genmddeps.c b/gcc/genmddeps.c
index fd26a33..e3d229d 100644
--- a/gcc/genmddeps.c
+++ b/gcc/genmddeps.c
@@ -47,7 +47,8 @@ main (int argc, const char **argv)
   progname = "genmddeps";
   include_callback = add_filedep;
 
-  if (!read_md_files (argc, argv, NULL, NULL))
+  noop_reader reader;
+  if (!reader.read_md_files (argc, argv, NULL))
     return FATAL_EXIT_CODE;
 
   *last = NULL;
diff --git a/gcc/gensupport.c b/gcc/gensupport.c
index 20603fd..901cc3d 100644
--- a/gcc/gensupport.c
+++ b/gcc/gensupport.c
@@ -2231,11 +2231,11 @@ process_define_subst (void)
 class gen_reader : public rtx_reader
 {
  public:
-  void handle_directive (file_location, const char *);
+  void handle_unknown_directive (file_location, const char *);
 };
 
 void
-gen_reader::handle_directive (file_location loc, const char *rtx_name)
+gen_reader::handle_unknown_directive (file_location loc, const char *rtx_name)
 {
   auto_vec<rtx, 32> subrtxs;
   if (!read_rtx (rtx_name, &subrtxs))
@@ -2523,7 +2523,7 @@ init_rtx_reader_args_cb (int argc, const char **argv,
   peephole2_sequence_num = 1;
 
   gen_reader reader;
-  read_md_files (argc, argv, parse_opt, &reader);
+  reader.read_md_files (argc, argv, parse_opt);
 
   if (define_attr_queue != NULL)
     check_define_attr_duplicates ();
diff --git a/gcc/read-md.c b/gcc/read-md.c
index a303752..ee6a67a 100644
--- a/gcc/read-md.c
+++ b/gcc/read-md.c
@@ -92,8 +92,6 @@ static htab_t md_constants;
 /* A table of enum_type structures, hashed by name.  */
 static htab_t enum_types;
 
-static void handle_file (rtx_reader *);
-
 /* Given an object that starts with a char * name field, return a hash
    code for its name.  */
 
@@ -905,8 +903,8 @@ traverse_enum_types (htab_trav callback, void *info)
    to process each unknown directive.  LINENO is the line number on
    which the "include" occurred.  */
 
-static void
-handle_include (file_location loc, rtx_reader *reader)
+void
+rtx_reader::handle_include (file_location loc)
 {
   const char *filename;
   const char *old_filename;
@@ -965,7 +963,7 @@ handle_include (file_location loc, rtx_reader *reader)
 
   read_md_file = input_file;
   read_md_filename = pathname;
-  handle_file (reader);
+  handle_file ();
 
   /* Restore the old cursor.  */
   read_md_file = old_file;
@@ -980,8 +978,8 @@ handle_include (file_location loc, rtx_reader *reader)
    read_md_filename are valid.  Use HANDLE_DIRECTIVE to handle
    unknown directives.  */
 
-static void
-handle_file (rtx_reader *reader)
+void
+rtx_reader::handle_file ()
 {
   struct md_name directive;
   int c;
@@ -1001,11 +999,9 @@ handle_file (rtx_reader *reader)
       else if (strcmp (directive.string, "define_c_enum") == 0)
 	handle_enum (loc, false);
       else if (strcmp (directive.string, "include") == 0)
-	handle_include (loc, reader);
-      else if (reader)
-	reader->handle_directive (loc, directive.string);
+	handle_include (loc);
       else
-	read_skip_construct (1, loc);
+	handle_unknown_directive (loc, directive.string);
 
       c = read_skip_spaces ();
       if (c != ')')
@@ -1017,8 +1013,8 @@ handle_file (rtx_reader *reader)
 /* Like handle_file, but for top-level files.  Set up in_fname and
    base_dir accordingly.  */
 
-static void
-handle_toplevel_file (rtx_reader *reader)
+void
+rtx_reader::handle_toplevel_file ()
 {
   const char *base;
 
@@ -1029,7 +1025,7 @@ handle_toplevel_file (rtx_reader *reader)
   else
     base_dir = xstrndup (in_fname, base - in_fname);
 
-  handle_file (reader);
+  handle_file ();
 }
 
 /* Parse a -I option with argument ARG.  */
@@ -1055,16 +1051,11 @@ parse_include (const char *arg)
 
    PARSE_OPT, if nonnull, is passed all unknown command-line arguments.
    It should return true if it recognizes the argument or false if a
-   generic error should be reported.
-
-   If HANDLE_DIRECTIVE is nonnull, the parser calls it for each
-   unknown directive, otherwise it just skips such directives.
-   See the comment above the directive_handler_t definition for
-   details about the callback's interface.  */
+   generic error should be reported.  */
 
 bool
-read_md_files (int argc, const char **argv, bool (*parse_opt) (const char *),
-	       rtx_reader *reader)
+rtx_reader::read_md_files (int argc, const char **argv,
+			   bool (*parse_opt) (const char *))
 {
   int i;
   bool no_more_options;
@@ -1134,7 +1125,7 @@ read_md_files (int argc, const char **argv, bool (*parse_opt) (const char *),
 
 	      read_md_file = stdin;
 	      read_md_filename = "<stdin>";
-	      handle_toplevel_file (reader);
+	      handle_toplevel_file ();
 	      already_read_stdin = true;
 	      continue;
 	    }
@@ -1157,7 +1148,7 @@ read_md_files (int argc, const char **argv, bool (*parse_opt) (const char *),
 	  perror (read_md_filename);
 	  return false;
 	}
-      handle_toplevel_file (reader);
+      handle_toplevel_file ();
       num_files++;
     }
 
@@ -1167,8 +1158,17 @@ read_md_files (int argc, const char **argv, bool (*parse_opt) (const char *),
     {
       read_md_file = stdin;
       read_md_filename = "<stdin>";
-      handle_toplevel_file (reader);
+      handle_toplevel_file ();
     }
 
   return !have_error;
 }
+
+/* class noop_reader : public rtx_reader */
+
+/* A dummy implementation which skips unknown directives.  */
+void
+noop_reader::handle_unknown_directive (file_location loc, const char *)
+{
+  read_skip_construct (1, loc);
+}
diff --git a/gcc/read-md.h b/gcc/read-md.h
index cb5c7ff..a9391e9 100644
--- a/gcc/read-md.h
+++ b/gcc/read-md.h
@@ -93,11 +93,25 @@ struct enum_type {
 class rtx_reader
 {
  public:
+  bool read_md_files (int, const char **, bool (*) (const char *));
+
   /* A hook that handles a single .md-file directive, up to but not
      including the closing ')'.  It takes two arguments: the file position
      at which the directive started, and the name of the directive.  The next
      unread character is the optional space after the directive name.  */
-  virtual void handle_directive (file_location, const char *) = 0;
+  virtual void handle_unknown_directive (file_location, const char *) = 0;
+
+ private:
+  void handle_file ();
+  void handle_toplevel_file ();
+  void handle_include (file_location loc);
+};
+
+class noop_reader : public rtx_reader
+{
+ public:
+  /* A dummy implementation which skips unknown directives.  */
+  void handle_unknown_directive (file_location, const char *);
 };
 
 extern const char *in_fname;
@@ -154,7 +168,5 @@ extern void upcase_string (char *);
 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 *);
-extern bool read_md_files (int, const char **, bool (*) (const char *),
-			   rtx_reader *reader);
 
 #endif /* GCC_READ_MD_H */
diff --git a/gcc/rtl/rtl-frontend.c b/gcc/rtl/rtl-frontend.c
index ee2e5d6..9bc0438 100644
--- a/gcc/rtl/rtl-frontend.c
+++ b/gcc/rtl/rtl-frontend.c
@@ -162,11 +162,11 @@ static hash_map<uid_hash, rtx> insns_by_uid;
 class function_reader : public rtx_reader
 {
  public:
-  void handle_directive (file_location, const char *);
+  void handle_unknown_directive (file_location, const char *);
 };
 
 void
-function_reader::handle_directive (file_location loc, const char *name)
+function_reader::handle_unknown_directive (file_location loc, const char *name)
 {
   rtx x = read_rtx_code (name);
   if (!x)
@@ -193,8 +193,7 @@ rtl_langhook_parse_file (void)
   for (unsigned i = 0; i < num_in_fnames; i++)
     argv.safe_push (in_fnames[i]);
   function_reader reader;
-  if (read_md_files (argv.length (), argv.address (), NULL,
-		     &reader))
+  if (reader.read_md_files (argv.length (), argv.address (), NULL))
     {
     }
   else
-- 
1.8.5.3

