diff options
author | Philip Herron <philip.herron@embecosm.com> | 2021-05-07 16:53:17 +0100 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2021-05-11 18:09:57 +0100 |
commit | 58e108c7c6d0fa3c9ebc7eeb681851924e22ddd5 (patch) | |
tree | 95c03868e9d05a84000e29187e2e5ccba431cd35 /gcc/rust/backend/rust-compile.cc | |
parent | c04343dbab902e5b8a5f047266645a5e7d40a5ed (diff) | |
download | gcc-58e108c7c6d0fa3c9ebc7eeb681851924e22ddd5.zip gcc-58e108c7c6d0fa3c9ebc7eeb681851924e22ddd5.tar.gz gcc-58e108c7c6d0fa3c9ebc7eeb681851924e22ddd5.tar.bz2 |
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
Diffstat (limited to 'gcc/rust/backend/rust-compile.cc')
-rw-r--r-- | gcc/rust/backend/rust-compile.cc | 68 |
1 files changed, 68 insertions, 0 deletions
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<const TyTy::ADTType *> (self); + std::string buf = s->get_identifier (); + + if (s->has_subsititions_defined ()) + { + buf += kMangledSubstBegin; + + const std::vector<TyTy::SubstitutionParamMapping> ¶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 |