aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-04-25 08:51:51 +0000
committerGitHub <noreply@github.com>2022-04-25 08:51:51 +0000
commitf562d738ef93964af16d0450fe56f2e84664efed (patch)
treeab07c3c043235a368fdb295d242df30b06c2d581
parent1286acc34e826039ebc8f09ad36dddc30f726283 (diff)
parenta969ab61adebf53d713144936c604ea55100c49d (diff)
downloadgcc-f562d738ef93964af16d0450fe56f2e84664efed.zip
gcc-f562d738ef93964af16d0450fe56f2e84664efed.tar.gz
gcc-f562d738ef93964af16d0450fe56f2e84664efed.tar.bz2
Merge #1149
1149: backend: handle no_mangle attribute r=philberty a=liushuyu - handle the `no_mangle` attribute Co-authored-by: liushuyu <liushuyu011@gmail.com>
-rw-r--r--gcc/rust/backend/rust-compile-base.cc48
-rw-r--r--gcc/rust/backend/rust-compile-base.h10
-rw-r--r--gcc/rust/util/rust-attributes.cc1
-rw-r--r--gcc/testsuite/rust/debug/no_mangle.rs17
4 files changed, 65 insertions, 11 deletions
diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc
index 84afa1e..4f55b22 100644
--- a/gcc/rust/backend/rust-compile-base.cc
+++ b/gcc/rust/backend/rust-compile-base.cc
@@ -26,14 +26,21 @@
#include "fold-const.h"
#include "stringpool.h"
+#include "attribs.h"
namespace Rust {
namespace Compile {
+bool inline should_mangle_item (const tree fndecl)
+{
+ 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
@@ -58,6 +65,7 @@ HIRCompileBase::setup_attributes_on_fndecl (
bool is_cold = attr.get_path ().as_string ().compare ("cold") == 0;
bool is_link_section
= attr.get_path ().as_string ().compare ("link_section") == 0;
+ bool no_mangle = attr.get_path ().as_string ().compare ("no_mangle") == 0;
if (is_inline)
{
handle_inline_attribute_on_fndecl (fndecl, attr);
@@ -74,6 +82,10 @@ HIRCompileBase::setup_attributes_on_fndecl (
{
handle_link_section_attribute_on_fndecl (fndecl, attr);
}
+ else if (no_mangle)
+ {
+ handle_no_mangle_attribute_on_fndecl (fndecl, attr);
+ }
}
}
@@ -121,6 +133,21 @@ HIRCompileBase::handle_link_section_attribute_on_fndecl (
}
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 %<no_mangle%> 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)
{
@@ -396,16 +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;
- if (!is_main_fn)
- 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 70506c2..c09c562 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);
diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc
index c9112eb..77f884e 100644
--- a/gcc/rust/util/rust-attributes.cc
+++ b/gcc/rust/util/rust-attributes.cc
@@ -32,6 +32,7 @@ static const BuiltinAttrDefinition __definitions[] = {
{"must_use", STATIC_ANALYSIS},
{"lang", HIR_LOWERING},
{"link_section", CODE_GENERATION},
+ {"no_mangle", CODE_GENERATION},
};
BuiltinAttributeMappings *
diff --git a/gcc/testsuite/rust/debug/no_mangle.rs b/gcc/testsuite/rust/debug/no_mangle.rs
new file mode 100644
index 0000000..0cef404
--- /dev/null
+++ b/gcc/testsuite/rust/debug/no_mangle.rs
@@ -0,0 +1,17 @@
+#[no_mangle]
+fn do_not_mangle() -> i32 {
+ 0
+}
+
+fn please_mangle() {}
+
+fn main() {
+// { dg-do compile }
+// { dg-options "-gdwarf-5 -dA" }
+ let _ = do_not_mangle();
+ please_mangle();
+// look for unmangled function name:
+// { dg-final { scan-assembler "do_not_mangle:" } } */
+// look for legacy mangled function name:
+// { dg-final { scan-assembler "13please_mangle" } } */
+}