From 8fc39d79e3b4292e887770b6dd92b94956a7c4cc Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Tue, 22 Mar 2016 18:24:49 -0400
Subject: [PATCH 007/117] FIXME: enough hacks to allow running read-rtl.c
 inside RTL frontend (which then fails)

---
 gcc/read-rtl.c         | 57 ++++++++++++++++++++++++++++++++++++++++----------
 gcc/rtl.h              |  3 +++
 gcc/rtl/Make-lang.in   |  3 ++-
 gcc/rtl/rtl-frontend.c |  7 +++++--
 4 files changed, 56 insertions(+), 14 deletions(-)

diff --git a/gcc/read-rtl.c b/gcc/read-rtl.c
index 79f42bf..46bd8e1 100644
--- a/gcc/read-rtl.c
+++ b/gcc/read-rtl.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
 
 /* Disable rtl checking; it conflicts with the iterator handling.  */
 #undef ENABLE_RTL_CHECKING
@@ -106,7 +112,7 @@ htab_t subst_attr_to_iter_map = NULL;
 const char *current_iterator_name;
 
 static void validate_const_int (const char *);
-static rtx read_rtx_code (const char *);
+static void one_time_initialization (void);
 static rtx read_nested_rtx (void);
 static rtx read_rtx_variadic (rtx);
 
@@ -181,6 +187,8 @@ apply_int_iterator (void *loc, int value)
   *(int *)loc = value;
 }
 
+#ifdef GENERATOR_FILE
+
 /* This routine adds attribute or does nothing depending on VALUE.  When
    VALUE is 1, it does nothing - the first duplicate of original
    template is kept untouched when it's subjected to a define_subst.
@@ -234,6 +242,7 @@ apply_subst_iterator (void *loc, int value)
     }
   XVEC (rt, 4) = new_attrs_vec;
 }
+#endif /* #ifdef GENERATOR_FILE */
 
 /* Map subst-attribute ATTR to subst iterator ITER.  */
 
@@ -418,6 +427,8 @@ copy_rtx_for_iterators (rtx original)
   return x;
 }
 
+#ifdef GENERATOR_FILE
+
 /* Return a condition that must satisfy both ORIGINAL and EXTRA.  If ORIGINAL
    has the form "&& ..." (as used in define_insn_and_splits), assume that
    EXTRA is already satisfied.  Empty strings are treated like "true".  */
@@ -581,6 +592,7 @@ apply_iterators (rtx original, vec<rtx> *queue)
 	}
     }
 }
+#endif /* #ifdef GENERATOR_FILE */
 
 /* Add a new "mapping" structure to hashtable TABLE.  NAME is the name
    of the mapping and GROUP is the group to which it belongs.  */
@@ -655,7 +667,9 @@ initialize_iterators (void)
   substs.iterators = htab_create (13, leading_string_hash,
 				 leading_string_eq_p, 0);
   substs.find_builtin = find_int; /* We don't use it, anyway.  */
+#ifdef GENERATOR_FILE
   substs.apply_iterator = apply_subst_iterator;
+#endif
 
   lower = add_mapping (&modes, modes.attrs, "mode");
   upper = add_mapping (&modes, modes.attrs, "MODE");
@@ -724,6 +738,8 @@ atoll (const char *p)
 }
 #endif
 
+
+#ifdef GENERATOR_FILE
 /* Process a define_conditions directive, starting with the optional
    space after the "define_conditions".  The directive looks like this:
 
@@ -771,6 +787,7 @@ read_conditions (void)
       add_c_test (expr, value);
     }
 }
+#endif /* #ifdef GENERATOR_FILE */
 
 static void
 validate_const_int (const char *string)
@@ -929,6 +946,7 @@ read_mapping (struct iterator_group *group, htab_t table)
    'yes' and 'no'.  This attribute is used to mark templates to which
    define_subst ATTR_NAME should be applied.  This attribute is set and
    defined implicitly and automatically.  */
+#ifdef GENERATOR_FILE
 static void
 add_define_attr_for_define_subst (const char *attr_name, vec<rtx> *queue)
 {
@@ -1030,14 +1048,7 @@ check_code_iterator (struct mapping *iterator)
 bool
 read_rtx (const char *rtx_name, vec<rtx> *rtxen)
 {
-  static bool initialized = false;
-
-  /* Do one-time initialization.  */
-  if (!initialized)
-    {
-      initialize_iterators ();
-      initialized = true;
-    }
+  one_time_initialization ();
 
   /* Handle various rtx-related declarations that aren't themselves
      encoded as rtxes.  */
@@ -1092,16 +1103,32 @@ read_rtx (const char *rtx_name, vec<rtx> *rtxen)
   return true;
 }
 
+#endif /* #ifdef GENERATOR_FILE */
+
+/* Do one-time initialization.  */
+
+static void
+one_time_initialization (void)
+{
+  static bool initialized = false;
+
+  if (!initialized)
+    {
+      initialize_iterators ();
+      initialized = true;
+    }
+}
+
 /* Subroutine of read_rtx and read_nested_rtx.  CODE_NAME is the name of
    either an rtx code or a code iterator.  Parse the rest of the rtx and
    return it.  */
 
-static rtx
+rtx
 read_rtx_code (const char *code_name)
 {
   int i;
   RTX_CODE code;
-  struct mapping *iterator, *m;
+  struct mapping *iterator = NULL, *m;
   const char *format_ptr;
   struct md_name name;
   rtx return_rtx;
@@ -1118,13 +1145,19 @@ read_rtx_code (const char *code_name)
       rtx value;		/* Value of this node.  */
     };
 
+  one_time_initialization ();
+
   /* If this code is an iterator, build the rtx using the iterator's
      first value.  */
+#ifdef GENERATOR_FILE
   iterator = (struct mapping *) htab_find (codes.iterators, &code_name);
   if (iterator != 0)
     code = (enum rtx_code) iterator->values->number;
   else
     code = (enum rtx_code) codes.find_builtin (code_name);
+#else
+    code = (enum rtx_code) codes.find_builtin (code_name);
+#endif
 
   /* If we end up with an insn expression then we free this space below.  */
   return_rtx = rtx_alloc (code);
@@ -1235,6 +1268,7 @@ read_rtx_code (const char *code_name)
 
 	  stringbuf = read_string (star_if_braced);
 
+#ifdef GENERATOR_FILE
 	  /* For insn patterns, we want to provide a default name
 	     based on the file and line, like "*foo.md:12", if the
 	     given name is blank.  These are only for define_insn and
@@ -1256,6 +1290,7 @@ read_rtx_code (const char *code_name)
 	      obstack_grow (&string_obstack, line_name, strlen (line_name)+1);
 	      stringbuf = XOBFINISH (&string_obstack, char *);
 	    }
+#endif /* #ifdef GENERATOR_FILE */
 
 	  /* Find attr-names in the string.  */
 	  ptr = &tmpstr[0];
diff --git a/gcc/rtl.h b/gcc/rtl.h
index c91d60d..3a19242 100644
--- a/gcc/rtl.h
+++ b/gcc/rtl.h
@@ -3639,7 +3639,10 @@ extern void init_varasm_once (void);
 extern rtx make_debug_expr_from_rtl (const_rtx);
 
 /* In read-rtl.c */
+#ifdef GENERATOR_FILE
 extern bool read_rtx (const char *, vec<rtx> *);
+#endif
+extern rtx read_rtx_code (const char *code_name);
 
 /* In alias.c */
 extern rtx canon_rtx (rtx);
diff --git a/gcc/rtl/Make-lang.in b/gcc/rtl/Make-lang.in
index 19d5842..8c913bc 100644
--- a/gcc/rtl/Make-lang.in
+++ b/gcc/rtl/Make-lang.in
@@ -31,7 +31,8 @@ rtl-warn = $(STRICT_WARN)
 RTL_OBJS = \
 	rtl/rtl-errors.o \
 	rtl/rtl-frontend.o \
-	read-md.o
+	read-md.o \
+	read-rtl.o
 
 rtl1$(exeext): $(RTL_OBJS) attribs.o $(BACKEND) $(LIBDEPS)
 	+$(LLINKER) $(ALL_LINKERFLAGS) $(LDFLAGS) -o $@ \
diff --git a/gcc/rtl/rtl-frontend.c b/gcc/rtl/rtl-frontend.c
index a36069e..cfb9e2e 100644
--- a/gcc/rtl/rtl-frontend.c
+++ b/gcc/rtl/rtl-frontend.c
@@ -35,6 +35,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "common/common-target.h"
 #include "read-md.h"
 #include <mpfr.h>
+#include "rtl.h"
 
 /* Language-dependent contents of a type.  */
 
@@ -155,11 +156,13 @@ rtl_langhook_handle_option (
   return true;
 }
 
-
 static void
 directive_handler (file_location loc, const char *name)
 {
-  fprintf (stderr, "directive: %s\n", name);
+  rtx x = read_rtx_code (name);
+  if (!x)
+    return;
+  debug (x);
 }
 
 static void
-- 
1.8.5.3

