From 1f9f0faec7b7627c5ea3e742db710554b2cfc0bf Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Thu, 7 Apr 2016 13:11:38 -0400
Subject: [PATCH 36/91] FIXME: consolidate fixup handling

---
 gcc/read-md.h          |   5 ++
 gcc/read-rtl.c         |  31 +------------
 gcc/rtl/rtl-frontend.c | 123 +++++++++++++++++++++++++++++++++++--------------
 3 files changed, 95 insertions(+), 64 deletions(-)

diff --git a/gcc/read-md.h b/gcc/read-md.h
index eb9e9b2..34e3313 100644
--- a/gcc/read-md.h
+++ b/gcc/read-md.h
@@ -95,6 +95,11 @@ class rtx_reader
      unread character is the optional space after the directive name.  */
   virtual void handle_unknown_directive (file_location, const char *) = 0;
 
+  /* FIXME.  */
+  virtual void add_fixup_insn_uid (file_location /*loc*/, rtx /*insn*/,
+				   int /*operand_idx*/, int /*insn_uid*/)
+  {}
+
   file_location get_current_location () const;
 
   void update_location (bool advancing);
diff --git a/gcc/read-rtl.c b/gcc/read-rtl.c
index 3173a16..b4a9664 100644
--- a/gcc/read-rtl.c
+++ b/gcc/read-rtl.c
@@ -1143,29 +1143,6 @@ parse_note_insn_name (const char *string)
 }
 #endif /* #ifndef GENERATOR_FILE */
 
-class fixup
-{
- public:
-  fixup (file_location loc, rtx insn, int operand_idx, int insn_uid)
-    : m_loc (loc), m_insn (insn), m_operand_idx (operand_idx),
-      m_insn_uid (insn_uid)
-  {}
-
-  file_location m_loc;
-  rtx m_insn;
-  int m_operand_idx;
-  int m_insn_uid;
-};
-static vec<fixup> fixups;
-
-void fixup_insn_refs (void (*cb) (file_location, rtx, int, int))
-{
-  int i;
-  fixup *f;
-  FOR_EACH_VEC_ELT (fixups, i, f)
-    (*cb) (f->m_loc, f->m_insn, f->m_operand_idx, f->m_insn_uid);
-}
-
 /* FIXME.  */
 
 #ifndef GENERATOR_FILE
@@ -1412,13 +1389,7 @@ read_rtx_code (const char *code_name)
 	  file_location loc = read_name (&name);
 	  int insn_id = atoi (name.string);
 	  if (insn_id)
-	    {
-	      fprintf (stderr,
-		       "FIXME: would store insn %i into field %i of %s %p\n",
-		       insn_id, i, code_name, (void *)return_rtx);
-	      fixup f (loc, return_rtx, i, insn_id);
-	      fixups.safe_push (f);
-	    }
+	    rtx_reader_ptr->add_fixup_insn_uid (loc, return_rtx, i, insn_id);
 	}
 #endif
 	break;
diff --git a/gcc/rtl/rtl-frontend.c b/gcc/rtl/rtl-frontend.c
index d4ddd1a..7b2ec01 100644
--- a/gcc/rtl/rtl-frontend.c
+++ b/gcc/rtl/rtl-frontend.c
@@ -156,31 +156,44 @@ rtl_langhook_handle_option (
   return true;
 }
 
-struct uid_hash : int_hash <int, -1, -2> {};
-static hash_map<uid_hash, rtx> insns_by_uid;
+class function_reader;
+
+class fixup
+{
+ public:
+  fixup (file_location loc, rtx insn, int operand_idx, int insn_uid)
+    : m_loc (loc), m_insn (insn), m_operand_idx (operand_idx),
+      m_insn_uid (insn_uid)
+  {}
+
+  void apply (function_reader *reader) const;
+
+ private:
+  file_location m_loc;
+  rtx m_insn;
+  int m_operand_idx;
+  int m_insn_uid;
+};
 
 class function_reader : public rtx_reader
 {
  public:
   function_reader (line_maps *set) : rtx_reader (set) {}
+  ~function_reader ();
   void handle_unknown_directive (file_location, const char *);
-};
 
-void
-function_reader::handle_unknown_directive (file_location start_loc,
-					   const char *name)
-{
-  rtx x = read_rtx_code (name);
-  if (!x)
-    return;
-  int c = read_char ();
-  location_t end_loc = get_current_location ();
-  unread_char (c);
-  location_t insn_loc = make_location (start_loc, start_loc, end_loc);
-  inform (insn_loc, "input insn");
-  debug (x);
-  insns_by_uid.put (INSN_UID(x), x);
-}
+  void add_fixup_insn_uid (file_location loc, rtx insn, int operand_idx,
+			   int insn_uid);
+
+  void apply_fixups ();
+
+  rtx *get_insn_by_uid (int uid);
+
+ private:
+  struct uid_hash : int_hash <int, -1, -2> {};
+  hash_map<uid_hash, rtx> m_insns_by_uid;
+  auto_vec<fixup *> m_fixups;
+};
 
 static const char *
 get_operand_name (rtx insn, int operand_idx)
@@ -197,26 +210,72 @@ get_operand_name (rtx insn, int operand_idx)
     }
 }
 
-static void
-fixup_cb (file_location loc, rtx insn, int operand_idx, int insn_uid)
+void
+fixup::apply (function_reader *reader) const
 {
-  rtx *insn_from_uid = insns_by_uid.get (insn_uid);
+  rtx *insn_from_uid = reader->get_insn_by_uid (m_insn_uid);
   if (insn_from_uid)
-    XEXP (insn, operand_idx) = *insn_from_uid;
+    XEXP (m_insn, m_operand_idx) = *insn_from_uid;
   else
     {
-      const char *op_name = get_operand_name (insn, operand_idx);
+      const char *op_name = get_operand_name (m_insn, m_operand_idx);
       if (op_name)
-	error_at (loc,
+	error_at (m_loc,
 		  "insn with UID %i not found for operand %i (%qs) of insn %i",
-		  insn_uid, operand_idx, op_name, INSN_UID (insn));
+		  m_insn_uid, m_operand_idx, op_name, INSN_UID (m_insn));
       else
-	error_at (loc,
+	error_at (m_loc,
 		  "insn with UID %i not found for operand %i of insn %i",
-		  insn_uid, operand_idx, INSN_UID (insn));
+		  m_insn_uid, m_operand_idx, INSN_UID (m_insn));
     }
 }
 
+function_reader::~function_reader ()
+{
+  int i;
+  fixup *f;
+  FOR_EACH_VEC_ELT (m_fixups, i, f)
+    delete f;
+}
+
+void
+function_reader::handle_unknown_directive (file_location start_loc,
+					   const char *name)
+{
+  rtx x = read_rtx_code (name);
+  if (!x)
+    return;
+  int c = read_char ();
+  location_t end_loc = get_current_location ();
+  unread_char (c);
+  location_t insn_loc = make_location (start_loc, start_loc, end_loc);
+  inform (insn_loc, "input insn");
+  debug (x);
+  m_insns_by_uid.put (INSN_UID(x), x);
+}
+
+void
+function_reader::add_fixup_insn_uid (file_location loc, rtx insn, int operand_idx,
+				     int insn_uid)
+{
+  m_fixups.safe_push (new fixup (loc, insn, operand_idx, insn_uid));
+}
+
+void
+function_reader::apply_fixups ()
+{
+  int i;
+  fixup *f;
+  FOR_EACH_VEC_ELT (m_fixups, i, f)
+    f->apply (this);
+}
+
+rtx *
+function_reader::get_insn_by_uid (int uid)
+{
+  return m_insns_by_uid.get (uid);
+}
+
 static void
 rtl_langhook_parse_file (void)
 {
@@ -225,14 +284,10 @@ rtl_langhook_parse_file (void)
   for (unsigned i = 0; i < num_in_fnames; i++)
     argv.safe_push (in_fnames[i]);
   function_reader reader (line_table);
-  if (reader.read_md_files (argv.length (), argv.address (), NULL))
-    {
-    }
-  else
-    {
-    }
+  if (!reader.read_md_files (argv.length (), argv.address (), NULL))
+    return;
 
-  fixup_insn_refs (fixup_cb);
+  reader.apply_fixups ();
 }
 
 static tree
-- 
1.8.5.3

