public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Jason Merrill <jason@redhat.com>
Cc: gcc-patches@gcc.gnu.org
Subject: [PATCH] c++: Fix up handling of register ... asm ("...") vars in templates [PR33661, PR98847]
Date: Thu, 28 Jan 2021 09:52:25 +0100	[thread overview]
Message-ID: <20210128085225.GE4020736@tucnak> (raw)

Hi!

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.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2021-01-27  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.

--- gcc/cp/decl.c.jj	2021-01-07 22:54:34.000000000 +0100
+++ gcc/cp/decl.c	2021-01-27 10:48:14.399582310 +0100
@@ -7840,6 +7840,12 @@ cp_finish_decl (tree decl, tree init, bo
 	  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;
     }
 
--- gcc/cp/pt.c.jj	2021-01-22 10:07:53.643466723 +0100
+++ gcc/cp/pt.c	2021-01-27 10:59:04.109126313 +0100
@@ -18227,6 +18227,7 @@ tsubst_expr (tree t, tree args, tsubst_f
 		    bool 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)
@@ -18250,7 +18251,18 @@ tsubst_expr (tree t, tree args, tsubst_f
 		       now.  */
 		    predeclare_vla (decl);
 
-		    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);
--- gcc/testsuite/g++.target/i386/pr98847.C.jj	2021-01-27 11:01:55.312161637 +0100
+++ gcc/testsuite/g++.target/i386/pr98847.C	2021-01-27 11:00:26.601179659 +0100
@@ -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;
+}

	Jakub


             reply	other threads:[~2021-01-28  8:52 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-28  8:52 Jakub Jelinek [this message]
2021-01-28 13:14 ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210128085225.GE4020736@tucnak \
    --to=jakub@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).