From: Stefan Schulze Frielinghaus <[email protected]>

Starting with r0-104773-g0d697034b7eecd which landed in GCC 4.6 (which
introduced initial C++11 and std::nullptr_t support), arguments of type
nullptr_t / std::nullptr_t / decltype(nullptr) are passed in registers.
However, return values are erroneously returned in memory violating the
psABI.

Fixed by handling NULLPTR_TYPE in s390_return_in_memory, similarly to
how it is already handled in s390_function_arg_integer, so that it is
returned in a register.

For C programs the effect/breakage is probably quite limited.  However,
I'm a bit worried about C++ code.  Any thoughts about this?  Clang
passes and returns correctly in registers.

Background: I stumbled across this when looking into PR126938 which
reminds me that I have to be on a lookout once META_TYPE becomes
available.

gcc/ChangeLog:

        * config/s390/s390.cc (s390_return_in_memory): Handle
        NULLPTR_TYPE.

gcc/testsuite/ChangeLog:

        * gcc.target/s390/nullptr-1.c: New test.
---
 gcc/config/s390/s390.cc                   |  1 +
 gcc/testsuite/gcc.target/s390/nullptr-1.c | 96 +++++++++++++++++++++++
 2 files changed, 97 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/s390/nullptr-1.c

diff --git a/gcc/config/s390/s390.cc b/gcc/config/s390/s390.cc
index d2459f5900a..a5d1a527a50 100644
--- a/gcc/config/s390/s390.cc
+++ b/gcc/config/s390/s390.cc
@@ -13427,6 +13427,7 @@ s390_return_in_memory (const_tree type, const_tree 
fundecl ATTRIBUTE_UNUSED)
   /* We accept small integral (and similar) types.  */
   if (INTEGRAL_TYPE_P (type)
       || POINTER_TYPE_P (type)
+      || TREE_CODE (type) == NULLPTR_TYPE
       || TREE_CODE (type) == OFFSET_TYPE
       || SCALAR_FLOAT_TYPE_P (type))
     return int_size_in_bytes (type) > 8;
diff --git a/gcc/testsuite/gcc.target/s390/nullptr-1.c 
b/gcc/testsuite/gcc.target/s390/nullptr-1.c
new file mode 100644
index 00000000000..c8129124828
--- /dev/null
+++ b/gcc/testsuite/gcc.target/s390/nullptr-1.c
@@ -0,0 +1,96 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -std=c23" } */
+/* { dg-final { check-function-bodies "**" "" "" } } */
+
+/* ABI tests for passing/returning values of type nullptr_t.  */
+
+#include <stddef.h>
+
+/*
+** test_ret:
+**     lghi    %r2,0
+**     br      %r14
+*/
+nullptr_t
+test_ret (void)
+{
+  return nullptr;
+}
+
+/*
+** test_arg_1_caller:
+**     ...
+**     lghi    %r2,0
+**     ...
+*/
+extern void test_arg_1 (nullptr_t);
+void
+test_arg_1_caller (void)
+{
+  test_arg_1 (nullptr);
+}
+
+/*
+** test_arg_2_caller:
+**     ...
+**     lghi    %r3,0
+**     ...
+*/
+extern void test_arg_2 (long, nullptr_t);
+void
+test_arg_2_caller (void)
+{
+  test_arg_2 (42, nullptr);
+}
+
+/*
+** test_arg_3_caller:
+**     ...
+**     lghi    %r4,0
+**     ...
+*/
+extern void test_arg_3 (long, long, nullptr_t);
+void
+test_arg_3_caller (void)
+{
+  test_arg_3 (42, 42, nullptr);
+}
+
+/*
+** test_arg_4_caller:
+**     ...
+**     lghi    %r5,0
+**     ...
+*/
+extern void test_arg_4 (long, long, long, nullptr_t);
+void
+test_arg_4_caller (void)
+{
+  test_arg_4 (42, 42, 42, nullptr);
+}
+
+/*
+** test_arg_5_caller:
+**     ...
+**     lghi    %r6,0
+**     ...
+*/
+extern void test_arg_5 (long, long, long, long, nullptr_t);
+void
+test_arg_5_caller (void)
+{
+  test_arg_5 (42, 42, 42, 42, nullptr);
+}
+
+/*
+** test_arg_6_caller:
+**     ...
+**     (mvghi  160\(%r15\),0|stg       %r[0-9]+,160\(%r15\))
+**     ...
+*/
+extern void test_arg_6 (long, long, long, long, long, nullptr_t);
+void
+test_arg_6_caller (void)
+{
+  test_arg_6 (42, 42, 42, 42, 42, nullptr);
+}
-- 
2.55.0

Reply via email to