From 5ec81137326fb7f4f5a7ac009405dbc6a80547cd Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Fri, 8 Apr 2016 02:16:06 -0400
Subject: [PATCH 044/101] FIXME: use deferred locations to implement fixup of
 INSN_LOCATION

---
 gcc/read-md.h          |  6 ++++++
 gcc/read-rtl.c         | 12 ++++++++----
 gcc/rtl/rtl-frontend.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 56 insertions(+), 4 deletions(-)

diff --git a/gcc/read-md.h b/gcc/read-md.h
index 9946e54..493d312 100644
--- a/gcc/read-md.h
+++ b/gcc/read-md.h
@@ -110,6 +110,12 @@ class rtx_reader
 						int /*bb_idx*/)
   {}
 
+  virtual void add_fixup_source_location (file_location /*loc*/, rtx /*insn*/,
+					  int /*operand_idx*/,
+					  const char */*filename*/,
+					  int /*lineno*/)
+  {}
+
   file_location get_current_location () const;
 
   void update_location (bool advancing);
diff --git a/gcc/read-rtl.c b/gcc/read-rtl.c
index 8e3feb5..1d7398b 100644
--- a/gcc/read-rtl.c
+++ b/gcc/read-rtl.c
@@ -1163,7 +1163,7 @@ parse_note_insn_name (const char *string)
     Assume that filenames can't contain whitespace, and can't
     contain ':'.  */
 
-void maybe_read_location ()
+void maybe_read_location (int operand_idx, rtx insn)
 {
   file_location loc = rtx_reader_ptr->get_current_location ();
 
@@ -1191,10 +1191,14 @@ void maybe_read_location ()
 	  return;
 	}
     }
-  inform (loc, "got filename: %qs", buf.address ());
+  char *filename = buf.address ();
+  //inform (loc, "got filename: %qs", filename);
   struct md_name name;
   read_name (&name);
-  inform (loc, "got line: %qs", name.string);
+  //inform (loc, "got line: %qs", name.string);
+
+  rtx_reader_ptr->add_fixup_source_location (loc, insn, operand_idx,
+					     filename, atoi(name.string));
 }
 
 #endif /* #ifndef GENERATOR_FILE */
@@ -1581,7 +1585,7 @@ read_rtx_code (const char *code_name)
 
 	  if (i == 4 && INSN_P (return_rtx))
 	    {
-	      maybe_read_location ();
+	      maybe_read_location (i, return_rtx);
 	      break;
 	    }
 
diff --git a/gcc/rtl/rtl-frontend.c b/gcc/rtl/rtl-frontend.c
index c511ccb..b433574 100644
--- a/gcc/rtl/rtl-frontend.c
+++ b/gcc/rtl/rtl-frontend.c
@@ -43,6 +43,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "cfg.h"
 #include "basic-block.h"
 #include "cfgrtl.h"
+#include "deferred-locations.h"
 
 /* Language-dependent contents of a type.  */
 
@@ -224,6 +225,22 @@ class fixup_note_insn_basic_block : public fixup
   int m_bb_idx;
 };
 
+class fixup_source_location : public fixup
+{
+ public:
+  fixup_source_location (file_location loc, rtx insn,
+			 int operand_idx,
+			 deferred_location *dloc)
+    : fixup (loc, insn, operand_idx),
+      m_dloc (dloc)
+  {}
+
+  void apply (function_reader *reader) const;
+
+ private:
+  deferred_location *m_dloc;
+};
+
 class function_reader : public rtx_reader
 {
  public:
@@ -243,6 +260,10 @@ class function_reader : public rtx_reader
   void add_fixup_note_insn_basic_block (file_location loc, rtx insn,
 					int operand_idx, int bb_idx);
 
+  void add_fixup_source_location (file_location loc, rtx insn,
+				  int operand_idx,
+				  const char *filename, int lineno);
+
   void create_function ();
   void apply_fixups ();
 
@@ -256,6 +277,7 @@ class function_reader : public rtx_reader
   auto_vec<fixup *> m_fixups;
   int m_max_bb_idx;
   rtx_insn *m_first_insn;
+  deferred_locations m_deferred_locations;
 };
 
 static const char *
@@ -314,6 +336,12 @@ fixup_note_insn_basic_block::apply (function_reader */*reader*/) const
   NOTE_BASIC_BLOCK (m_insn) = bb;
 }
 
+void
+fixup_source_location::apply (function_reader */*reader*/) const
+{
+  INSN_LOCATION (as_a <rtx_insn *> (m_insn)) = m_dloc->m_srcloc;
+}
+
 function_reader::~function_reader ()
 {
   int i;
@@ -364,6 +392,16 @@ function_reader::add_fixup_note_insn_basic_block (file_location loc, rtx insn,
 						       bb_idx));
 }
 
+void
+function_reader::add_fixup_source_location (file_location loc, rtx insn,
+					    int operand_idx,
+					    const char *filename, int lineno)
+{
+  source_file *file = m_deferred_locations.get_source_file (filename);
+  source_line *line = file->get_source_line (lineno);
+  deferred_location *dloc = line->get_location (NULL, 0);
+  m_fixups.safe_push (new fixup_source_location (loc, insn, operand_idx, dloc));
+}
 
 void
 function_reader::create_function ()
@@ -441,6 +479,10 @@ function_reader::create_function ()
 void
 function_reader::apply_fixups ()
 {
+  m_deferred_locations.add_to_line_table ();
+  /* line_table should now be populated; every deferred_location should
+     now have an m_srcloc.  */
+
   int i;
   fixup *f;
   FOR_EACH_VEC_ELT (m_fixups, i, f)
-- 
1.8.5.3

