From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id BC0E6386DC6A; Wed, 8 Jun 2022 12:36:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BC0E6386DC6A Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] backend: address comments about no_mangle X-Act-Checkin: gcc X-Git-Author: liushuyu X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: af7622f219871cdaf8ae7e124008730b411d7a06 X-Git-Newrev: a969ab61adebf53d713144936c604ea55100c49d Message-Id: <20220608123636.BC0E6386DC6A@sourceware.org> Date: Wed, 8 Jun 2022 12:36:36 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 12:36:36 -0000 https://gcc.gnu.org/g:a969ab61adebf53d713144936c604ea55100c49d commit a969ab61adebf53d713144936c604ea55100c49d Author: liushuyu Date: Fri Apr 22 04:28:39 2022 -0600 backend: address comments about no_mangle Diff: --- gcc/rust/backend/rust-compile-base.cc | 61 +++++++++++++++++++---------------- gcc/rust/backend/rust-compile-base.h | 10 ++++-- 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index 0c28728e5e1..4f55b22f761 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -26,32 +26,21 @@ #include "fold-const.h" #include "stringpool.h" +#include "attribs.h" namespace Rust { namespace Compile { -bool -should_mangle_item (const AST::AttrVec &attrs) +bool inline should_mangle_item (const tree fndecl) { - for (const auto &attr : attrs) - { - if (attr.get_path ().as_string ().compare ("no_mangle") == 0) - { - if (attr.has_attr_input ()) - rust_error_at ( - attr.get_locus (), - "attribute % does not accept any arguments"); - return false; - } - } - - return true; + return lookup_attribute ("no_mangle", DECL_ATTRIBUTES (fndecl)) == NULL_TREE; } void -HIRCompileBase::setup_attributes_on_fndecl ( - tree fndecl, bool is_main_entry_point, HIR::Visibility &visibility, - const HIR::FunctionQualifiers &qualifiers, const AST::AttrVec &attrs) +HIRCompileBase::setup_fndecl (tree fndecl, bool is_main_entry_point, + HIR::Visibility &visibility, + const HIR::FunctionQualifiers &qualifiers, + const AST::AttrVec &attrs) { // if its the main fn or pub visibility mark its as DECL_PUBLIC // please see https://github.com/Rust-GCC/gccrs/pull/137 @@ -95,8 +84,7 @@ HIRCompileBase::setup_attributes_on_fndecl ( } else if (no_mangle) { - // we handled this in `should_mangle_item` - continue; + handle_no_mangle_attribute_on_fndecl (fndecl, attr); } } } @@ -144,6 +132,21 @@ HIRCompileBase::handle_link_section_attribute_on_fndecl ( set_decl_section_name (fndecl, msg_str.c_str ()); } +void +HIRCompileBase::handle_no_mangle_attribute_on_fndecl ( + tree fndecl, const AST::Attribute &attr) +{ + if (attr.has_attr_input ()) + { + rust_error_at (attr.get_locus (), + "attribute % does not accept any arguments"); + return; + } + + DECL_ATTRIBUTES (fndecl) = tree_cons (get_identifier ("no_mangle"), NULL_TREE, + DECL_ATTRIBUTES (fndecl)); +} + void HIRCompileBase::handle_inline_attribute_on_fndecl (tree fndecl, const AST::Attribute &attr) @@ -420,19 +423,21 @@ HIRCompileBase::compile_function ( // we don't mangle the main fn since we haven't implemented the main shim bool is_main_fn = fn_name.compare ("main") == 0; std::string asm_name = fn_name; - // TODO(liushuyu): we should probably move this part to - // `setup_attributes_on_fndecl` if possible - bool should_mangle = should_mangle_item (outer_attrs); - if (!is_main_fn && should_mangle) - asm_name = ctx->mangle_item (fntype, *canonical_path); unsigned int flags = 0; tree fndecl = ctx->get_backend ()->function (compiled_fn_type, ir_symbol_name, - asm_name, flags, locus); - setup_attributes_on_fndecl (fndecl, is_main_fn, visibility, qualifiers, - outer_attrs); + "" /* asm_name */, flags, locus); + setup_fndecl (fndecl, is_main_fn, visibility, qualifiers, outer_attrs); setup_abi_options (fndecl, fntype->get_abi ()); + // conditionally mangle the function name + bool should_mangle = should_mangle_item (fndecl); + if (!is_main_fn && should_mangle) + asm_name = ctx->mangle_item (fntype, *canonical_path); + SET_DECL_ASSEMBLER_NAME (fndecl, + get_identifier_with_length (asm_name.data (), + asm_name.length ())); + // insert into the context ctx->insert_function_decl (fntype, fndecl); diff --git a/gcc/rust/backend/rust-compile-base.h b/gcc/rust/backend/rust-compile-base.h index 70506c2c7c0..c09c5620e53 100644 --- a/gcc/rust/backend/rust-compile-base.h +++ b/gcc/rust/backend/rust-compile-base.h @@ -75,9 +75,10 @@ protected: tree resolve_unsized_adjustment (Resolver::Adjustment &adjustment, tree expression, Location locus); - static void setup_attributes_on_fndecl ( - tree fndecl, bool is_main_entry_point, HIR::Visibility &visibility, - const HIR::FunctionQualifiers &qualifiers, const AST::AttrVec &attrs); + static void setup_fndecl (tree fndecl, bool is_main_entry_point, + HIR::Visibility &visibility, + const HIR::FunctionQualifiers &qualifiers, + const AST::AttrVec &attrs); static void handle_inline_attribute_on_fndecl (tree fndecl, const AST::Attribute &attr); @@ -92,6 +93,9 @@ protected: handle_link_section_attribute_on_fndecl (tree fndecl, const AST::Attribute &attr); + static void handle_no_mangle_attribute_on_fndecl (tree fndecl, + const AST::Attribute &attr); + static void setup_abi_options (tree fndecl, ABI abi); static tree address_expression (tree, Location);