From patchwork Thu Jun 23 23:55:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: jesus X-Patchwork-Id: 55352 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 4ABEF386C589 for ; Thu, 23 Jun 2022 23:56:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4ABEF386C589 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1656028565; bh=luYCLzCvboONdae6V8dNqz0a5wQC5g79S6oxrKVYXTE=; h=Date:Subject:To:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=tFPZywS6gQAN2IkNqJoQy7iau+MRr2Rfluq0KbYA+JbLiRU0VHYP/8sArcp6hQaV0 IjT1eIY6Z4UrwV2EiruYgkrYPiOAd8YtR6XIj0VEZr/FDzLqjCs/ESQ5h4uEuWwJp+ NvCku+3B6U5mSOcE6fkMxJ6h7m7R06n5iiWiwDgI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oa1-x2e.google.com (mail-oa1-x2e.google.com [IPv6:2001:4860:4864:20::2e]) by sourceware.org (Postfix) with ESMTPS id 50E5A3854162 for ; Thu, 23 Jun 2022 23:55:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 50E5A3854162 Received: by mail-oa1-x2e.google.com with SMTP id 586e51a60fabf-f2a4c51c45so1564553fac.9 for ; Thu, 23 Jun 2022 16:55:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=luYCLzCvboONdae6V8dNqz0a5wQC5g79S6oxrKVYXTE=; b=e3qoAFXWKetvVgePgdVmPH3MmLJ0+b80HWqlaCJSGY/wcW5Of4a3qGnJ1Xy+gOKV5l 1+Oms2PZvEQPzZup0R5C8S9UV5W3PvPv7M5k3pWeAA5StWkgDiSCFxuI2K1AY1g0Gmfs GJsTaHtFzkheCA3E81hUxkT5mRPY6QuP22zFng4T2T26i+yb+2Poq5Nem5S6KbvKP0I+ nrNC9XKayE1N9Rfihg3gnK7OrA0EAGIhCPX4IVL2J7itrkdrWWnS8jML0vSl2LO8KJfu 8XvconfC+71DzdD5NduYflrUIEtE1UcTHeP65Mn8GefGoWKRIjL6XiSDKfUgeLTO5CCN jnFQ== X-Gm-Message-State: AJIora/uSrYT1fogXQU72/60w3etIHXtnWEYc8tet0qbUtpAtgqD/eWd Xw/RTwiQ5OMbyDcvgJds0wF034BkqSvaR8eRGbSKKwXq X-Google-Smtp-Source: AGRyM1tvF/oUNC9xxYKXYsls0qggYIPFILzJ8ASCuSLCjiNNIZZHYE8FOSk9s+XaCvXA8tREIUzjqlNT8/5jY+AhR/Y= X-Received: by 2002:a05:6870:4413:b0:e9:a163:4ffd with SMTP id u19-20020a056870441300b000e9a1634ffdmr286802oah.245.1656028533196; Thu, 23 Jun 2022 16:55:33 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a05:6359:f28:b0:a5:6982:5c5d with HTTP; Thu, 23 Jun 2022 16:55:32 -0700 (PDT) Date: Thu, 23 Jun 2022 16:55:32 -0700 Message-ID: Subject: [PATCH] or1k: gcc: Add naked attribute To: gcc-patches@gcc.gnu.org X-Spam-Status: No, score=-6.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: jesus via Gcc-patches From: jesus Reply-To: jesus Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Hello. I have added support for naked functions on the OpenRISC 1200 target it practically inhibits the generation of a function epilogue and prologue and will warn about variables that might use the stack (to prevent unintended code being generated). As well added a table for attributes where the attribute can only be applied functions, like in the other backends. gcc/ChangeLog: * config/or1k/or1k.cc (or1k_handle_naked_attribute): (has_func_attr): Likewise. (callee_saved_regno_p): Likewise. (or1k_save_reg): Likewise. (or1k_restore_reg): Likewise. (or1k_expand_prologue): Likewise. (or1k_expand_epilogue): Likewise. (or1k_frame_pointer_required): Likewise. (TARGET_ATTRIBUTE_TABLE): Define. --- gcc/config/or1k/or1k.cc | 58 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/gcc/config/or1k/or1k.cc b/gcc/config/or1k/or1k.cc index da2f59062ba..c41ac5ab099 100644 --- a/gcc/config/or1k/or1k.cc +++ b/gcc/config/or1k/or1k.cc @@ -79,6 +79,41 @@ struct GTY(()) machine_function rtx_insn *set_mcount_arg_insn; }; +static tree +or1k_handle_naked_attribute(tree *node, tree name ATTRIBUTE_UNUSED, + tree args ATTRIBUTE_UNUSED, + int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) +{ + gcc_assert(DECL_P(*node)); + + if (TREE_CODE(*node) != FUNCTION_DECL) + { + warning(OPT_Wattributes, "%qE attribute only applies to functions", + name); + *no_add_attrs = true; + } + return NULL_TREE; +} + +static const struct attribute_spec or1k_attribute_table[] = { + { "naked", 0, 0, true, false, false, false, + or1k_handle_naked_attribute, NULL }, + + /* End element. */ + { NULL, 0, 0, false, false, false, false, NULL, NULL } +}; + +/* Returns true if the provided function has the specified attribute. */ + +static inline bool +has_func_attr(const_tree decl, const char *func_attr) +{ + if (decl == NULL_TREE) + decl = current_function_decl; + + return lookup_attribute(func_attr, DECL_ATTRIBUTES(decl)) != NULL_TREE; +} + /* Zero initialization is OK for all current fields. */ static struct machine_function * @@ -103,6 +138,10 @@ or1k_option_override (void) static bool callee_saved_regno_p (int regno) { + /* Naked functions do not save anything, so let's say NO! */ + if (has_func_attr(NULL_TREE, "naked")) + return false; + /* Check call-saved registers. */ if (!call_used_or_fixed_reg_p (regno) && df_regs_ever_live_p (regno)) return true; @@ -185,6 +224,9 @@ or1k_compute_frame_layout (void) static void or1k_save_reg (int regno, HOST_WIDE_INT offset) { + if (has_func_attr(NULL_TREE, "naked")) + warning(0, "stack usage on naked function %s", current_function_name()); + rtx reg = gen_rtx_REG (Pmode, regno); rtx mem = gen_frame_mem (SImode, plus_constant (Pmode, stack_pointer_rtx, offset)); @@ -198,6 +240,9 @@ or1k_save_reg (int regno, HOST_WIDE_INT offset) static rtx or1k_restore_reg (int regno, HOST_WIDE_INT offset, rtx cfa_restores) { + if (has_func_attr(NULL_TREE, "naked")) + warning(0, "stack usage on naked function %s", current_function_name()); + rtx reg = gen_rtx_REG (Pmode, regno); rtx mem = gen_frame_mem (SImode, plus_constant (Pmode, stack_pointer_rtx, offset)); @@ -217,8 +262,8 @@ or1k_expand_prologue (void) if (flag_stack_usage_info) current_function_static_stack_size = -sp_offset; - /* Early exit for frameless functions. */ - if (sp_offset == 0) + /* Early exit for frameless functions */ + if (sp_offset == 0 || has_func_attr(NULL_TREE, "naked")) goto fini; /* Adjust the stack pointer. For large stack offsets we will @@ -325,7 +370,7 @@ or1k_expand_epilogue (void) rtx insn, cfa_restores = NULL; sp_offset = cfun->machine->total_size; - if (sp_offset == 0) + if (sp_offset == 0 || has_func_attr(NULL_TREE, "naked")) return; reg_offset = cfun->machine->local_vars_size + cfun->machine->args_size; @@ -509,6 +554,10 @@ or1k_return_addr (int, rtx frame) static bool or1k_frame_pointer_required () { + /* Frame pointer is not required for naked functions */ + if (has_func_attr(NULL_TREE, "naked")) + return false; + /* ??? While IRA checks accesses_prior_frames, reload does not. We do want the frame pointer for this case. */ return (crtl->accesses_prior_frames); @@ -2212,6 +2261,9 @@ or1k_output_mi_thunk (FILE *file, tree thunk_fndecl, #undef TARGET_HAVE_SPECULATION_SAFE_VALUE #define TARGET_HAVE_SPECULATION_SAFE_VALUE speculation_safe_value_not_needed +#undef TARGET_ATTRIBUTE_TABLE +#define TARGET_ATTRIBUTE_TABLE or1k_attribute_table + /* Calling Conventions. */ #undef TARGET_FUNCTION_VALUE #define TARGET_FUNCTION_VALUE or1k_function_value