From 58e108c7c6d0fa3c9ebc7eeb681851924e22ddd5 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Fri, 7 May 2021 16:53:17 +0100 Subject: Implement basic rustc legacy symbol mangling Rust supports two different symbol mangling methods legacy and V0. V0 is the goal but its not yet stable. This implements the legacy method but with a dummy hash value since it requires a sip128 implementation to generate the apropriate hash which can be done in a sperate change. This change allows us to actually assemble generic functions and avoid bad symbol duplications. Addresses #305 --- gcc/rust/backend/rust-compile.cc | 68 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) (limited to 'gcc/rust/backend/rust-compile.cc') diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc index 4893541..9dd3b57 100644 --- a/gcc/rust/backend/rust-compile.cc +++ b/gcc/rust/backend/rust-compile.cc @@ -413,5 +413,73 @@ HIRCompileBase::compile_function_body ( } } +// Mr Mangle time + +static const std::string kMangledSymbolPrefix = "_ZN"; +static const std::string kMangledSymbolDelim = "E"; +static const std::string kMangledGenericDelim = "$C$"; +static const std::string kMangledSubstBegin = "$LT$"; +static const std::string kMangledSubstEnd = "$GT$"; + +static std::string +mangle_name (const std::string &name) +{ + return std::to_string (name.size ()) + name; +} + +static std::string +dummy_hash () +{ + return "h0123456789abcdef"; +} + +static std::string +mangle_self (const TyTy::BaseType *self) +{ + if (self->get_kind () != TyTy::TypeKind::ADT) + return mangle_name (self->get_name ()); + + const TyTy::ADTType *s = static_cast (self); + std::string buf = s->get_identifier (); + + if (s->has_subsititions_defined ()) + { + buf += kMangledSubstBegin; + + const std::vector ¶ms + = s->get_substs (); + for (size_t i = 0; i < params.size (); i++) + { + const TyTy::SubstitutionParamMapping &sub = params.at (i); + buf += sub.as_string (); + + if ((i + 1) < params.size ()) + buf += kMangledGenericDelim; + } + + buf += kMangledSubstEnd; + } + + return mangle_name (buf); +} + +std::string +Context::mangle_item (const std::string &name) const +{ + const std::string &crate_name = mappings->get_current_crate_name (); + return kMangledSymbolPrefix + mangle_name (crate_name) + mangle_name (name) + + mangle_name (dummy_hash ()) + kMangledSymbolDelim; +} + +std::string +Context::mangle_impl_item (const TyTy::BaseType *self, + const std::string &name) const +{ + const std::string &crate_name = mappings->get_current_crate_name (); + return kMangledSymbolPrefix + mangle_name (crate_name) + mangle_self (self) + + mangle_name (name) + mangle_name (dummy_hash ()) + + kMangledSymbolDelim; +} + } // namespace Compile } // namespace Rust -- cgit v1.1