public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r9-9412] c++: Fix up handling of register ... asm ("...") vars in templates [PR33661, PR98847]
@ 2021-04-20 23:31 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2021-04-20 23:31 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:ef5db37cc4e80b229502bea7d6e2daa95ad6f805

commit r9-9412-gef5db37cc4e80b229502bea7d6e2daa95ad6f805
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Thu Jan 28 16:13:11 2021 +0100

    c++: Fix up handling of register ... asm ("...") vars in templates [PR33661, PR98847]
    
    As the testcase shows, for vars appearing in templates, we don't attach
    the asm spec string to the pattern decls, nor pass it back to cp_finish_decl
    during instantiation.
    
    The following patch does that.
    
    2021-01-28  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/33661
            PR c++/98847
            * decl.c (cp_finish_decl): For register vars with asmspec in templates
            call set_user_assembler_name and set DECL_HARD_REGISTER.
            * pt.c (tsubst_expr): When instantiating DECL_HARD_REGISTER vars,
            pass asmspec_tree to cp_finish_decl.
    
            * g++.target/i386/pr98847.C: New test.
    
    (cherry picked from commit cf93f94b3498f3925895fb0bbfd4b64232b9987a)

Diff:
---
 gcc/cp/decl.c                           |  6 ++++++
 gcc/cp/pt.c                             | 14 +++++++++++++-
 gcc/testsuite/g++.target/i386/pr98847.C | 20 ++++++++++++++++++++
 3 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index ec18f87e47f..3d51daeec30 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -7138,6 +7138,12 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
 	  retrofit_lang_decl (decl);
 	  SET_DECL_DEPENDENT_INIT_P (decl, true);
 	}
+
+      if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec)
+	{
+	  set_user_assembler_name (decl, asmspec);
+	  DECL_HARD_REGISTER (decl) = 1;
+	}
       return;
     }
 
diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index be3863c1522..86b13f9d2bd 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -17310,6 +17310,7 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
 		    int const_init = false;
 		    unsigned int cnt = 0;
 		    tree first = NULL_TREE, ndecl = error_mark_node;
+		    tree asmspec_tree = NULL_TREE;
 		    maybe_push_decl (decl);
 
 		    if (VAR_P (decl)
@@ -17328,7 +17329,18 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
 		    if (ndecl != error_mark_node)
 		      cp_maybe_mangle_decomp (ndecl, first, cnt);
 
-		    cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
+		    if (VAR_P (decl) && DECL_HARD_REGISTER (pattern_decl))
+		      {
+			tree id = DECL_ASSEMBLER_NAME (pattern_decl);
+			const char *asmspec = IDENTIFIER_POINTER (id);
+			gcc_assert (asmspec[0] == '*');
+			asmspec_tree
+			  = build_string (IDENTIFIER_LENGTH (id) - 1,
+					  asmspec + 1);
+			TREE_TYPE (asmspec_tree) = char_array_type_node;
+		      }
+
+		    cp_finish_decl (decl, init, const_init, asmspec_tree, 0);
 
 		    if (ndecl != error_mark_node)
 		      cp_finish_decomp (ndecl, first, cnt);
diff --git a/gcc/testsuite/g++.target/i386/pr98847.C b/gcc/testsuite/g++.target/i386/pr98847.C
new file mode 100644
index 00000000000..0a72cccafd6
--- /dev/null
+++ b/gcc/testsuite/g++.target/i386/pr98847.C
@@ -0,0 +1,20 @@
+// PR c++/98847
+// { dg-do run }
+// { dg-options "-O2 -masm=att" }
+
+template <int N>
+int
+foo ()
+{
+  register int edx asm ("edx");
+  asm ("movl $1234, %%edx" : "=r" (edx));
+  return edx;
+}
+
+int
+main ()
+{
+  if (foo<0> () != 1234)
+    __builtin_abort ();
+  return 0;
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-04-20 23:31 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-20 23:31 [gcc r9-9412] c++: Fix up handling of register ... asm ("...") vars in templates [PR33661, PR98847] Jakub Jelinek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).