aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorliushuyu <liushuyu011@gmail.com>2022-04-21 17:19:27 -0600
committerliushuyu <liushuyu011@gmail.com>2022-04-22 16:16:37 -0600
commitaf7622f219871cdaf8ae7e124008730b411d7a06 (patch)
treec0da8f48c2cd78c60c784c52c3381be371d6237e /gcc
parent1286acc34e826039ebc8f09ad36dddc30f726283 (diff)
downloadgcc-af7622f219871cdaf8ae7e124008730b411d7a06.zip
gcc-af7622f219871cdaf8ae7e124008730b411d7a06.tar.gz
gcc-af7622f219871cdaf8ae7e124008730b411d7a06.tar.bz2
backend: handle no_mangle attribute
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/backend/rust-compile-base.cc29
-rw-r--r--gcc/rust/util/rust-attributes.cc1
-rw-r--r--gcc/testsuite/rust/debug/no_mangle.rs17
3 files changed, 46 insertions, 1 deletions
diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc
index 84afa1e..0c28728 100644
--- a/gcc/rust/backend/rust-compile-base.cc
+++ b/gcc/rust/backend/rust-compile-base.cc
@@ -30,6 +30,24 @@
namespace Rust {
namespace Compile {
+bool
+should_mangle_item (const AST::AttrVec &attrs)
+{
+ 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 %<no_mangle%> does not accept any arguments");
+ return false;
+ }
+ }
+
+ return true;
+}
+
void
HIRCompileBase::setup_attributes_on_fndecl (
tree fndecl, bool is_main_entry_point, HIR::Visibility &visibility,
@@ -58,6 +76,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 +93,11 @@ HIRCompileBase::setup_attributes_on_fndecl (
{
handle_link_section_attribute_on_fndecl (fndecl, attr);
}
+ else if (no_mangle)
+ {
+ // we handled this in `should_mangle_item`
+ continue;
+ }
}
}
@@ -396,7 +420,10 @@ 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)
+ // 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;
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" } } */
+}