From bc006bc8dd4b3ebab4a0afb0eb2d2c2ce1728cf3 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Mon, 11 Apr 2016 16:04:17 -0400
Subject: [PATCH 062/101] FIXME: handle MEM_EXPR, in theory, at least

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

diff --git a/gcc/read-md.h b/gcc/read-md.h
index 97745b8..c085574 100644
--- a/gcc/read-md.h
+++ b/gcc/read-md.h
@@ -121,8 +121,8 @@ class rtx_reader
 				     const char */*label*/)
   {}
 
-  virtual void add_fixup_reg_expr (file_location /*loc*/, rtx /*insn*/,
-				   const char */*desc*/)
+  virtual void add_fixup_expr (file_location /*loc*/, rtx /*x*/,
+			       const char */*desc*/)
   {}
 
   file_location get_current_location () const;
diff --git a/gcc/read-rtl.c b/gcc/read-rtl.c
index 7d8a892..2bcdc5e 100644
--- a/gcc/read-rtl.c
+++ b/gcc/read-rtl.c
@@ -1659,7 +1659,7 @@ read_rtx_code (const char *code_name)
 	      file_location loc = rtx_reader_ptr->get_current_location ();
 	      char *desc = read_until ("]", true);
 	      strip_trailing_whitespace (desc);
-	      rtx_reader_ptr->add_fixup_reg_expr (loc, return_rtx, desc);
+	      rtx_reader_ptr->add_fixup_expr (loc, return_rtx, desc);
 	      free (desc);
 	    }
 	  else
@@ -1686,6 +1686,7 @@ read_rtx_code (const char *code_name)
 	    {
 	      file_location loc = rtx_reader_ptr->get_current_location ();
 	      char *desc = read_until (" +", false);
+	      rtx_reader_ptr->add_fixup_expr (loc, return_rtx, desc);
 	      inform (loc, "got desc for MEM_EXPR of %qs", desc);
 	      // FIXME: do something with it.
 	      free (desc);
diff --git a/gcc/rtl/rtl-frontend.c b/gcc/rtl/rtl-frontend.c
index f868764..4faf949 100644
--- a/gcc/rtl/rtl-frontend.c
+++ b/gcc/rtl/rtl-frontend.c
@@ -273,11 +273,11 @@ class fixup_jump_label : public operand_fixup
   const char *m_label;
 };
 
-class fixup_reg_expr : public fixup
+class fixup_expr : public fixup
 {
  public:
-  fixup_reg_expr (file_location loc, rtx insn, const char *desc)
-    : fixup (loc, insn),
+  fixup_expr (file_location loc, rtx x, const char *desc)
+    : fixup (loc, x),
       m_desc (xstrdup (desc))
   {}
 
@@ -314,8 +314,8 @@ class function_reader : public rtx_reader
 			     int operand_idx,
 			     const char *label);
 
-  void add_fixup_reg_expr (file_location loc, rtx insn,
-			   const char *desc);
+  void add_fixup_expr (file_location loc, rtx x,
+		       const char *desc);
 
   void create_function ();
   void apply_fixups ();
@@ -324,6 +324,8 @@ class function_reader : public rtx_reader
 
   rtx_insn *get_first_insn () const { return m_first_insn; }
 
+  tree parse_mem_expr (const char *desc);
+
  private:
   struct uid_hash : int_hash <int, -1, -2> {};
   hash_map<uid_hash, rtx_insn *> m_insns_by_uid;
@@ -331,6 +333,7 @@ class function_reader : public rtx_reader
   int m_max_bb_idx;
   rtx_insn *m_first_insn;
   deferred_locations m_deferred_locations;
+  auto_vec<tree> m_params;
 };
 
 static const char *
@@ -418,8 +421,8 @@ fixup_jump_label::apply (function_reader *reader) const
     }
 }
 
-static tree
-parse_mem_expr (const char *desc)
+tree
+function_reader::parse_mem_expr (const char *desc)
 {
   if (0 == strcmp (desc, "<retval>"))
     {
@@ -427,15 +430,32 @@ parse_mem_expr (const char *desc)
       return DECL_RESULT (fndecl);
     }
 
+  int i;
+  tree t;
+  FOR_EACH_VEC_ELT (m_params, i, t)
+    if (0 == strcmp (desc, IDENTIFIER_POINTER (DECL_NAME (t))))
+      return t;
+
   return NULL_TREE;
 }
 
 void
-fixup_reg_expr::apply (function_reader */*reader*/) const
+fixup_expr::apply (function_reader *reader) const
 {
-  //inform (m_loc, "fixup_reg_expr::apply: %qs", m_desc);
-  tree expr = parse_mem_expr (m_desc);
-  set_reg_attrs_for_decl_rtl (expr, m_insn);
+  inform (m_loc, "fixup_expr::apply: %qs", m_desc);
+  tree expr = reader->parse_mem_expr (m_desc);
+  switch (GET_CODE (m_insn))
+    {
+    case REG:
+      set_reg_attrs_for_decl_rtl (expr, m_insn);
+      break;
+    case MEM:
+      set_mem_expr (m_insn, expr);
+      break;
+    default:
+      gcc_unreachable ();
+    }
+
   /* FIXME: not actually an insn. */
 }
 
@@ -514,10 +534,10 @@ function_reader::add_fixup_jump_label (file_location loc, rtx insn,
 }
 
 void
-function_reader::add_fixup_reg_expr (file_location loc, rtx insn,
-				     const char *desc)
+function_reader::add_fixup_expr (file_location loc, rtx insn,
+				 const char *desc)
 {
-  m_fixups.safe_push (new fixup_reg_expr (loc, insn, desc));
+  m_fixups.safe_push (new fixup_expr (loc, insn, desc));
 }
 
 void
@@ -547,6 +567,14 @@ function_reader::create_function ()
   allocate_struct_function (fndecl, false);
   /* This sets cfun.  */
 
+  /* What about params?  */
+  m_params.safe_push (build_decl (UNKNOWN_LOCATION, PARM_DECL,
+				  get_identifier ("i"), int_type));
+  m_params.safe_push (build_decl (UNKNOWN_LOCATION, PARM_DECL,
+				  get_identifier ("j"), int_type));
+  m_params.safe_push (build_decl (UNKNOWN_LOCATION, PARM_DECL,
+				  get_identifier ("k"), int_type));
+
   //struct function *fn = ; // DECL_STRUCT_FUNCTION (node->decl);
   //push_cfun (fn);
 
-- 
1.8.5.3

