backend: handle no_mangle attribute

This commit is contained in:
liushuyu 2022-04-21 17:19:27 -06:00
parent 1286acc34e
commit af7622f219
3 changed files with 46 additions and 1 deletions

View File

@ -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;

View File

@ -32,6 +32,7 @@ static const BuiltinAttrDefinition __definitions[] = {
{"must_use", STATIC_ANALYSIS},
{"lang", HIR_LOWERING},
{"link_section", CODE_GENERATION},
{"no_mangle", CODE_GENERATION},
};
BuiltinAttributeMappings *

View File

@ -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" } } */
}