From patchwork Mon Feb 19 09:13:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Torbjorn SVENSSON X-Patchwork-Id: 85959 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4753F384DED1 for ; Mon, 19 Feb 2024 09:14:35 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx07-00178001.pphosted.com (mx07-00178001.pphosted.com [185.132.182.106]) by sourceware.org (Postfix) with ESMTPS id 292323861877 for ; Mon, 19 Feb 2024 09:14:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 292323861877 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=foss.st.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=foss.st.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 292323861877 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=185.132.182.106 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708334045; cv=none; b=aX8XtJGrp3SrY7eKvbvRXuf+m/z+3NSTGIbqnYCJqdct75uj9CTg3YXni8llx9q4FTHXgloIeI2DxqsbqOGaLSl7R2igmhJI27FfW9lWOy7WMamssbnhnRV3GZt2fnHbv9NQXXpaZk63+knikelVmd/EDBseVn0qr9mxgzIHVCk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708334045; c=relaxed/simple; bh=VL4EuE2mgsVQD9PpKEJJX3lXlrZhoq61aOJQO9Ai3mc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=DN6Ol+F6FEO/1KA8iffKxhtHkxhJDkHcsYHlsSmzr/XYgtGEW2k/rCNd0WWcFPcJSx53xLcGRKMRmA4ZT/eOodG1VhVziktZsXFNq8OVc40Vwl10viqj+BGuJUr6duntOovIh2RpwWqkOGqCufG64DZXQ5/8R9NKKr363rKVRD4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0369458.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.17.1.24/8.17.1.24) with ESMTP id 41J1V9eI011680; Mon, 19 Feb 2024 10:14:01 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foss.st.com; h= from:to:cc:subject:date:message-id:mime-version:content-type :content-transfer-encoding; s=selector1; bh=kPtBpeuyRwZb6iMcI+1+ m0xjms6Qwcn+Gk5c5GVXvS0=; b=UigR/sOxpZLPG6Y17t2vUYul3H+imMvXWV3B 5oWFRFh1lLrUYmf/461YASBKEjMNy3aBqMkuJKawnwn7pe3Gk/iqsDm42WNSKH3z dYoN6cgs2NgtBc4KGkfYeLG2e9F2h31EVHK6dgUIEkEhHbfTSPid6QkG9lLgBuNQ 7cURBa65ez3/bl5owsBONzHJix2Rs/tTNxtp3p0xOmvwl1VZOzEgxyrdJRSjuRrh hz8wxjQ18FWvmBYw/CWW2K6y4ADupCTnbOmIKbGFDV9vWO9HNlNwvtTBv6qx361L 2tqD7LBDzF+85JDBlXfZRUO8aptK87PLdfnvlpxZ2wpJi6Rs0g== Received: from beta.dmz-ap.st.com (beta.dmz-ap.st.com [138.198.100.35]) by mx07-00178001.pphosted.com (PPS) with ESMTPS id 3wb784bu0m-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Feb 2024 10:14:01 +0100 (CET) Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-ap.st.com (STMicroelectronics) with ESMTP id EF38840044; Mon, 19 Feb 2024 10:13:56 +0100 (CET) Received: from Webmail-eu.st.com (shfdag1node3.st.com [10.75.129.71]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id 75B55248814; Mon, 19 Feb 2024 10:13:40 +0100 (CET) Received: from jkgcxl0004.jkg.st.com (10.74.22.255) by SHFDAG1NODE3.st.com (10.75.129.71) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Mon, 19 Feb 2024 10:13:39 +0100 From: =?utf-8?q?Torbj=C3=B6rn_SVENSSON?= To: CC: , , , =?utf-8?q?Torbj=C3=B6rn_SVENSSON?= , Yvan ROUX Subject: [PATCH] arm: Fixed C23 call compatibility with arm-none-eabi Date: Mon, 19 Feb 2024 10:13:07 +0100 Message-ID: <20240219091306.523967-1-torbjorn.svensson@foss.st.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Originating-IP: [10.74.22.255] X-ClientProxiedBy: EQNCAS1NODE3.st.com (10.75.129.80) To SHFDAG1NODE3.st.com (10.75.129.71) X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-02-19_06,2024-02-16_01,2023-05-22_02 X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Ok for trunk and releases/gcc-13? Regtested on top of 945cb8490cb for arm-none-eabi, without any regression. Backporting to releases/gcc-13 will change -std=c23 to -std=c2x. --- In commit 4fe34cdcc80ac225b80670eabc38ac5e31ce8a5a, -std=c23 support was introduced to support functions without any named arguments. For arm-none-eabi, this is not as simple as placing all arguments on the stack. Align the caller to use r0, r1, r2 and r3 for arguments even for functions without any named arguments, as specified in the AAPCS. Verify that the generic test case have the arguments are in the right order and add ARM specific test cases. gcc/ChangeLog: * calls.h: Added the type of the function to function_arg_info. * calls.cc: Save the type of the function. * config/arm/arm.cc: Check in the AAPCS layout function if function has no named args. gcc/testsuite/ChangeLog: * gcc.dg/torture/c23-stdarg-split-1a.c: Detect out of order arguments. * gcc.dg/torture/c23-stdarg-split-1b.c: Likewise. * gcc.target/arm/aapcs/align_vaarg3.c: New test. * gcc.target/arm/aapcs/align_vaarg4.c: New test. Signed-off-by: Torbjörn SVENSSON Co-authored-by: Yvan ROUX --- gcc/calls.cc | 2 +- gcc/calls.h | 20 ++++++++-- gcc/config/arm/arm.cc | 13 ++++--- .../gcc.dg/torture/c23-stdarg-split-1a.c | 4 +- .../gcc.dg/torture/c23-stdarg-split-1b.c | 15 +++++--- .../gcc.target/arm/aapcs/align_vaarg3.c | 37 +++++++++++++++++++ .../gcc.target/arm/aapcs/align_vaarg4.c | 31 ++++++++++++++++ 7 files changed, 102 insertions(+), 20 deletions(-) create mode 100644 gcc/testsuite/gcc.target/arm/aapcs/align_vaarg3.c create mode 100644 gcc/testsuite/gcc.target/arm/aapcs/align_vaarg4.c diff --git a/gcc/calls.cc b/gcc/calls.cc index 01f44734743..a1cc283b952 100644 --- a/gcc/calls.cc +++ b/gcc/calls.cc @@ -1376,7 +1376,7 @@ initialize_argument_information (int num_actuals ATTRIBUTE_UNUSED, with those made by function.cc. */ /* See if this argument should be passed by invisible reference. */ - function_arg_info arg (type, argpos < n_named_args); + function_arg_info arg (type, fntype, argpos < n_named_args); if (pass_by_reference (args_so_far_pnt, arg)) { const bool callee_copies diff --git a/gcc/calls.h b/gcc/calls.h index 464a4e34e33..88836559ebe 100644 --- a/gcc/calls.h +++ b/gcc/calls.h @@ -35,24 +35,33 @@ class function_arg_info { public: function_arg_info () - : type (NULL_TREE), mode (VOIDmode), named (false), + : type (NULL_TREE), fntype (NULL_TREE), mode (VOIDmode), named (false), pass_by_reference (false) {} /* Initialize an argument of mode MODE, either before or after promotion. */ function_arg_info (machine_mode mode, bool named) - : type (NULL_TREE), mode (mode), named (named), pass_by_reference (false) + : type (NULL_TREE), fntype (NULL_TREE), mode (mode), named (named), + pass_by_reference (false) {} /* Initialize an unpromoted argument of type TYPE. */ function_arg_info (tree type, bool named) - : type (type), mode (TYPE_MODE (type)), named (named), + : type (type), fntype (NULL_TREE), mode (TYPE_MODE (type)), named (named), pass_by_reference (false) {} + /* Initialize an unpromoted argument of type TYPE with a known function type + FNTYPE. */ + function_arg_info (tree type, tree fntype, bool named) + : type (type), fntype (fntype), mode (TYPE_MODE (type)), named (named), + pass_by_reference (false) + {} + /* Initialize an argument with explicit properties. */ function_arg_info (tree type, machine_mode mode, bool named) - : type (type), mode (mode), named (named), pass_by_reference (false) + : type (type), fntype (NULL_TREE), mode (mode), named (named), + pass_by_reference (false) {} /* Return true if the gimple-level type is an aggregate. */ @@ -96,6 +105,9 @@ public: libgcc support functions). */ tree type; + /* The type of the function that has this argument, or null if not known. */ + tree fntype; + /* The mode of the argument. Depending on context, this might be the mode of the argument type or the mode after promotion. */ machine_mode mode; diff --git a/gcc/config/arm/arm.cc b/gcc/config/arm/arm.cc index 1cd69268ee9..98e149e5b7e 100644 --- a/gcc/config/arm/arm.cc +++ b/gcc/config/arm/arm.cc @@ -7006,7 +7006,7 @@ aapcs_libcall_value (machine_mode mode) numbers referred to here are those in the AAPCS. */ static void aapcs_layout_arg (CUMULATIVE_ARGS *pcum, machine_mode mode, - const_tree type, bool named) + const_tree type, bool named, const_tree fntype) { int nregs, nregs2; int ncrn; @@ -7018,8 +7018,9 @@ aapcs_layout_arg (CUMULATIVE_ARGS *pcum, machine_mode mode, pcum->aapcs_arg_processed = true; /* Special case: if named is false then we are handling an incoming - anonymous argument which is on the stack. */ - if (!named) + anonymous argument which is on the stack unless the function has no named + arguments (functions without named arguments was introduced in C23). */ + if ((fntype == NULL_TREE || !TYPE_NO_NAMED_ARGS_STDARG_P (fntype)) && !named) return; /* Is this a potential co-processor register candidate? */ @@ -7267,7 +7268,7 @@ arm_function_arg (cumulative_args_t pcum_v, const function_arg_info &arg) if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL) { - aapcs_layout_arg (pcum, arg.mode, arg.type, arg.named); + aapcs_layout_arg (pcum, arg.mode, arg.type, arg.named, arg.fntype); return pcum->aapcs_reg; } @@ -7342,7 +7343,7 @@ arm_arg_partial_bytes (cumulative_args_t pcum_v, const function_arg_info &arg) if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL) { - aapcs_layout_arg (pcum, arg.mode, arg.type, arg.named); + aapcs_layout_arg (pcum, arg.mode, arg.type, arg.named, arg.fntype); return pcum->aapcs_partial; } @@ -7367,7 +7368,7 @@ arm_function_arg_advance (cumulative_args_t pcum_v, if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL) { - aapcs_layout_arg (pcum, arg.mode, arg.type, arg.named); + aapcs_layout_arg (pcum, arg.mode, arg.type, arg.named, arg.fntype); if (pcum->aapcs_cprc_slot >= 0) { diff --git a/gcc/testsuite/gcc.dg/torture/c23-stdarg-split-1a.c b/gcc/testsuite/gcc.dg/torture/c23-stdarg-split-1a.c index 2dff79235b2..717fc4bfd07 100644 --- a/gcc/testsuite/gcc.dg/torture/c23-stdarg-split-1a.c +++ b/gcc/testsuite/gcc.dg/torture/c23-stdarg-split-1a.c @@ -5,7 +5,6 @@ /* { dg-options "-std=c23 -pedantic-errors" } */ /* { dg-additional-sources "c23-stdarg-split-1b.c" } */ -extern void abort (void); extern void exit (int); double f (...); @@ -22,8 +21,7 @@ void h7 (volatile struct s x, ...); int main () { - if (f (1, 2.0, 3, 4.0) != 10.0) - abort (); + f (1, 2.0, 3, 4.0); g (0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0); g (0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f); h1 (0, 0.0, 1, 2.0, 3, 4.0, 5, 6.0, 7, 8.0, 9); diff --git a/gcc/testsuite/gcc.dg/torture/c23-stdarg-split-1b.c b/gcc/testsuite/gcc.dg/torture/c23-stdarg-split-1b.c index 064da121ec2..3b70fe1595a 100644 --- a/gcc/testsuite/gcc.dg/torture/c23-stdarg-split-1b.c +++ b/gcc/testsuite/gcc.dg/torture/c23-stdarg-split-1b.c @@ -8,17 +8,20 @@ extern void abort (void); -double +void f (...) { va_list ap; va_start (ap); - double ret = va_arg (ap, int); - ret += va_arg (ap, double); - ret += va_arg (ap, int); - ret += va_arg (ap, double); + if (va_arg (ap, int) != 1) + abort(); + if (va_arg (ap, double) != 2) + abort(); + if (va_arg (ap, int) != 3) + abort(); + if (va_arg (ap, double) != 4) + abort(); va_end (ap); - return ret; } void diff --git a/gcc/testsuite/gcc.target/arm/aapcs/align_vaarg3.c b/gcc/testsuite/gcc.target/arm/aapcs/align_vaarg3.c new file mode 100644 index 00000000000..23cc78d5230 --- /dev/null +++ b/gcc/testsuite/gcc.target/arm/aapcs/align_vaarg3.c @@ -0,0 +1,37 @@ +/* Test AAPCS layout (alignment of varargs) for callee. */ + +/* { dg-do run { target arm_eabi } } */ +/* { dg-require-effective-target arm32 } */ +/* { dg-options "-std=c23 -O2 -fno-inline" } */ + +#include + +extern void abort (void); + +typedef __attribute__((aligned (8))) int alignedint; + +void +foo (...) +{ + int i = 0; + va_list va; + va_start (va); + /* Arguments should be passed in the same registers as if they were ints. */ + for (i = 5; i >= 0; i--) + if (va_arg (va, int) != i) + abort (); + va_end (va); +} + +int +main (int argc, char **argv) +{ + alignedint a = 5; + alignedint b = 4; + alignedint c = 3; + alignedint d = 2; + alignedint e = 1; + alignedint f = 0; + foo (a, b, c, d, e, f); + return 0; +} diff --git a/gcc/testsuite/gcc.target/arm/aapcs/align_vaarg4.c b/gcc/testsuite/gcc.target/arm/aapcs/align_vaarg4.c new file mode 100644 index 00000000000..c4333f33677 --- /dev/null +++ b/gcc/testsuite/gcc.target/arm/aapcs/align_vaarg4.c @@ -0,0 +1,31 @@ +/* Test AAPCS layout (alignment of varargs) for callee. */ + +/* { dg-do run { target arm_eabi } } */ +/* { dg-require-effective-target arm32 } */ +/* { dg-options "-std=c23 -O2 -fno-inline" } */ + +#include + +extern void abort (void); + +typedef __attribute__((aligned (8))) int alignedint; + +void +foo (...) +{ + int i = 0; + va_list va; + va_start (va); + /* alignedint should be pulled out of regs/stack just like an int. */ + for (i = 5; i >= 0; i--) + if (va_arg (va, alignedint) != i) + abort (); + va_end (va); +} + +int +main (int argc, char **argv) +{ + foo (5, 4, 3, 2, 1, 0); + return 0; +}