From 2b1c8860e8e990a175063dcc027dc4fb80356f2a Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Tue, 20 Sep 2016 10:38:33 -0400
Subject: [PATCH 22/42] FIXME: hackish debugging of parser

---
 gcc/read-md.c           | 54 +++++++++++++++++++++++++++++++++++++++++++++++--
 gcc/read-md.h           | 25 +++++++++++++++++++++++
 gcc/read-rtl-function.c |  2 ++
 gcc/read-rtl-function.h |  3 ++-
 gcc/read-rtl.c          |  2 ++
 gcc/selftest-rtl.c      |  3 ++-
 gcc/selftest-rtl.h      |  2 +-
 7 files changed, 86 insertions(+), 5 deletions(-)

diff --git a/gcc/read-md.c b/gcc/read-md.c
index 8e15e9b..8fab494 100644
--- a/gcc/read-md.c
+++ b/gcc/read-md.c
@@ -326,6 +326,8 @@ fatal_expected_char (int expected, int actual)
 int
 read_skip_spaces (void)
 {
+  LOG_SCOPE0 ("read_skip_spaces");
+
   int c;
 
   while (1)
@@ -373,6 +375,8 @@ read_skip_spaces (void)
 
 void require_char (char expected)
 {
+  LOG_SCOPE1 ("require_char: '%c'", expected);
+
   int ch = read_char ();
   if (ch != expected)
     fatal_expected_char (expected, ch);
@@ -384,6 +388,8 @@ void require_char (char expected)
 void
 require_char_ws (char expected)
 {
+  LOG_SCOPE1 ("require_char_ws: '%c'", expected);
+
   int ch = read_skip_spaces ();
   if (ch != expected)
     fatal_expected_char (expected, ch);
@@ -394,6 +400,8 @@ require_char_ws (char expected)
 
 void require_word_ws (const char *expected)
 {
+  LOG_SCOPE1 ("require_word_ws: \"%s\"", expected);
+
   struct md_name name;
   read_name (&name);
   if (strcmp (name.string, expected))
@@ -417,6 +425,8 @@ base_rtx_reader::read_char (void)
   else
     m_read_md_colno++;
 
+  LOG_SCOPE1 ("read_char: '%c'", ch);
+
   /* If we're filtering lines, treat everything outside the
      range of interest as a space.  */
   if (m_first_line && m_last_line)
@@ -443,6 +453,8 @@ base_rtx_reader::unread_char (int ch)
   else
     m_read_md_colno--;
   ungetc (ch, m_read_md_file);
+
+  LOG_SCOPE1 ("unread_char: '%c'", ch);
 }
 
 /* Peek at the next character from the file without consuming it.  */
@@ -450,8 +462,11 @@ base_rtx_reader::unread_char (int ch)
 int
 peek_char (void)
 {
+  LOG_SCOPE0 ("peek_char");
+
   int ch = read_char ();
   unread_char (ch);
+
   return ch;
 }
 
@@ -533,15 +548,20 @@ read_name_1 (struct md_name *name, file_location *out_loc)
 file_location
 read_name (struct md_name *name)
 {
+  LOG_SCOPE0 ("read_name");
+
   file_location loc;
   if (!read_name_1 (name, &loc))
     fatal_with_file_and_line ("missing name or number");
+
   return loc;
 }
 
 file_location
 read_name_or_nil (struct md_name *name)
 {
+  LOG_SCOPE0 ("read_name_or_nil");
+
   file_location loc;
   if (!read_name_1 (name, &loc))
     {
@@ -552,6 +572,7 @@ read_name_or_nil (struct md_name *name)
       name->buffer[0] = 0;
       name->string = name->buffer;
     }
+
   return loc;
 }
 
@@ -616,6 +637,8 @@ read_quoted_string (void)
 {
   int c;
 
+  LOG_SCOPE0 ("read_quoted_string");
+
   while (1)
     {
       c = read_char (); /* Read the string  */
@@ -645,6 +668,8 @@ read_braced_string (void)
   int brace_depth = 1;  /* caller-processed */
   unsigned long starting_read_md_lineno = base_rtx_reader_ptr->get_lineno ();
 
+  LOG_SCOPE0 ("read_braced_string");
+
   obstack_1grow (&string_obstack, '{');
   while (brace_depth)
     {
@@ -678,6 +703,8 @@ read_braced_string (void)
 char *
 read_string (int star_if_braced)
 {
+  LOG_SCOPE0 ("read_string");
+
   char *stringbuf;
   int saw_paren = 0;
   int c, old_lineno;
@@ -725,6 +752,8 @@ read_skip_construct (int depth, file_location loc)
   struct md_name name;
   int c;
 
+  LOG_SCOPE0 ("read_skip_construct");
+
   do
     {
       c = read_skip_spaces ();
@@ -737,10 +766,14 @@ read_skip_construct (int depth, file_location loc)
 	{
 	case '(':
 	  depth++;
+	  base_rtx_reader_ptr->log ("depth++;");
+	  base_rtx_reader_ptr->indent ();
 	  break;
 
 	case ')':
 	  depth--;
+	  base_rtx_reader_ptr->unindent ();
+	  base_rtx_reader_ptr->log ("depth--;");
 	  break;
 
 	case ':':
@@ -1001,7 +1034,8 @@ traverse_enum_types (htab_trav callback, void *info)
 /* Constructor for base_rtx_reader.  */
 
 base_rtx_reader::base_rtx_reader ()
-: m_toplevel_fname (NULL),
+: m_verbose (false),
+  m_toplevel_fname (NULL),
   m_base_dir (NULL),
   m_read_md_file (NULL),
   m_read_md_filename (NULL),
@@ -1010,7 +1044,8 @@ base_rtx_reader::base_rtx_reader ()
   m_first_dir_md_include (NULL),
   m_last_dir_md_include_ptr (&m_first_dir_md_include),
   m_first_line (0),
-  m_last_line (0)
+  m_last_line (0),
+  m_log_indent (0)
 {
   /* Set the global singleton pointer.  */
   base_rtx_reader_ptr = this;
@@ -1345,3 +1380,18 @@ noop_reader::handle_unknown_directive (file_location loc, const char *)
 {
   read_skip_construct (1, loc);
 }
+
+void
+base_rtx_reader::log (const char *fmt, ...)
+{
+  if (m_verbose)
+    {
+      for (int i = 0; i < m_log_indent; i++)
+	fputc (' ', stderr);
+      va_list ap;
+      va_start (ap, fmt);
+      vfprintf (stderr, fmt, ap);
+      va_end (ap);
+      fputc ('\n', stderr);
+    }
+}
diff --git a/gcc/read-md.h b/gcc/read-md.h
index 50fa616..335ea2f 100644
--- a/gcc/read-md.h
+++ b/gcc/read-md.h
@@ -115,6 +115,10 @@ class base_rtx_reader
   int get_lineno () const { return m_read_md_lineno; }
   int get_colno () const { return m_read_md_colno; }
 
+  void log (const char *fmt, ...);
+  void indent () { m_log_indent++; }
+  void unindent () { m_log_indent--; }
+
  private:
   /* A singly-linked list of filenames.  */
   struct file_name_list {
@@ -128,6 +132,9 @@ class base_rtx_reader
   void handle_include (file_location loc);
   void add_include_path (const char *arg);
 
+ protected:
+  bool m_verbose;
+
  private:
   /* The name of the toplevel file that indirectly included
      m_read_md_file.  */
@@ -163,11 +170,29 @@ class base_rtx_reader
   /* FIXME.  */
   int m_first_line;
   int m_last_line;
+
+  int m_log_indent;
 };
 
 /* Global singleton; constrast with rtx_reader_ptr below.  */
 extern base_rtx_reader *base_rtx_reader_ptr;
 
+/* FIXME.  */
+class log_scope
+{
+ public:
+  log_scope () { base_rtx_reader_ptr->indent (); }
+  ~log_scope () { base_rtx_reader_ptr->unindent (); }
+};
+
+#define LOG_SCOPE0(FMT) \
+  base_rtx_reader_ptr->log (FMT); \
+  log_scope scope;
+
+#define LOG_SCOPE1(FMT, ARG0)				\
+  base_rtx_reader_ptr->log ((FMT), (ARG0));			\
+  log_scope scope;
+
 /* A base_rtx_reader subclass which skips unknown directives, for
    the gen* tools that purely use read-md.o.  */
 
diff --git a/gcc/read-rtl-function.c b/gcc/read-rtl-function.c
index 335f749..80b52e8 100644
--- a/gcc/read-rtl-function.c
+++ b/gcc/read-rtl-function.c
@@ -656,6 +656,8 @@ function_reader::function_reader (function_reader_policy *policy)
   m_have_cfg_directive (false),
   m_have_crtl_directive (false)
 {
+  if (m_policy)
+    m_verbose = m_policy->m_verbose;
   bitmap_obstack_initialize (&m_bitmap_obstack);
   bitmap_initialize (&m_bb_indices_in_insns, &m_bitmap_obstack);
   bitmap_initialize (&m_bb_indices_in_cfg, &m_bitmap_obstack);
diff --git a/gcc/read-rtl-function.h b/gcc/read-rtl-function.h
index 7b8639b..5ae83ed 100644
--- a/gcc/read-rtl-function.h
+++ b/gcc/read-rtl-function.h
@@ -42,11 +42,12 @@ class regno_remapper
 struct function_reader_policy
 {
   function_reader_policy (regno_remapper *regno_remapper_)
-  : m_regno_remapper (regno_remapper_)
+  : m_regno_remapper (regno_remapper_), m_verbose (false)
   {
   }
 
   regno_remapper *m_regno_remapper;
+  bool m_verbose;
 };
 
 extern bool read_rtl_function_body (int argc, const char **argv,
diff --git a/gcc/read-rtl.c b/gcc/read-rtl.c
index 3f42ce7..cb04ff6 100644
--- a/gcc/read-rtl.c
+++ b/gcc/read-rtl.c
@@ -1218,6 +1218,8 @@ rtx_reader::read_rtx_code (const char *code_name)
       rtx value;		/* Value of this node.  */
     };
 
+  LOG_SCOPE1 ("read_rtx_code: %s", code_name);
+
   one_time_initialization ();
 
   /* If this code is an iterator, build the rtx using the iterator's
diff --git a/gcc/selftest-rtl.c b/gcc/selftest-rtl.c
index 20d0b92..2c3be93a 100644
--- a/gcc/selftest-rtl.c
+++ b/gcc/selftest-rtl.c
@@ -38,12 +38,13 @@ namespace selftest {
    Use LOC as the effective location when reporting failures.  */
 
 rtl_dump_test::rtl_dump_test (const location &loc, const char *path,
-			      int dumped_first_pseudo_regno)
+			      int dumped_first_pseudo_regno, bool verbose)
 : /* Remap registers >= dumped_first_pseudo_regno to be target-appropriate
      pseudos, for the target's current value of LAST_VIRTUAL_REGISTER.  */
   m_regno_remapper (dumped_first_pseudo_regno)
 {
   function_reader_policy policy (&m_regno_remapper);
+  policy.m_verbose = verbose;
 
   /* Parse the tempfile.  */
   auto_vec<const char *> argv (2);
diff --git a/gcc/selftest-rtl.h b/gcc/selftest-rtl.h
index b9cf605..ab3f9b8 100644
--- a/gcc/selftest-rtl.h
+++ b/gcc/selftest-rtl.h
@@ -38,7 +38,7 @@ class rtl_dump_test
 {
  public:
   rtl_dump_test (const location &loc, const char *path,
-		 int dumped_first_pseudo_regno = 0);
+		 int dumped_first_pseudo_regno = 0, bool verbose = false);
   ~rtl_dump_test ();
 
   unsigned int effective_regno (int input_regno) const;
-- 
1.8.5.3

