diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-09-08 12:23:49 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-08 12:23:49 +0000 |
commit | 52fb10b1e0ffe4f026ad9728c757c5e41ebac865 (patch) | |
tree | 70e1e0d3107c9307da1cda36f69f9b6a68ad2c02 /gcc/rust/backend | |
parent | 8a9271e1921d0eac46bda889da6508cf94d32682 (diff) | |
parent | 37f3b02bf8edbf707e6156feec593b9b0d7da027 (diff) | |
download | gcc-52fb10b1e0ffe4f026ad9728c757c5e41ebac865.zip gcc-52fb10b1e0ffe4f026ad9728c757c5e41ebac865.tar.gz gcc-52fb10b1e0ffe4f026ad9728c757c5e41ebac865.tar.bz2 |
Merge #656
656: Add mangling switch r=philberty a=CohenArthur
Add option to choose mangling scheme.
Closes #429
This PR splits the `Mangler` class in its own set of header and source and adds the base for v0 name mangling.
You are now able to specify the mangling scheme to use using `-frust-mangling=<value>`.
When inputting an invalid value, the compiler errors out using `unrecognized command-line option `-frust-mangling=<not_valid>`. Is there a better way to do this? Is there also a way to test this behaviour using dejagnu?
Co-authored-by: CohenArthur <arthur.cohen@epita.fr>
Diffstat (limited to 'gcc/rust/backend')
-rw-r--r-- | gcc/rust/backend/rust-compile-context.h | 16 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile.cc | 103 | ||||
-rw-r--r-- | gcc/rust/backend/rust-mangle.cc | 152 | ||||
-rw-r--r-- | gcc/rust/backend/rust-mangle.h | 54 |
4 files changed, 218 insertions, 107 deletions
diff --git a/gcc/rust/backend/rust-compile-context.h b/gcc/rust/backend/rust-compile-context.h index bffe97c..05c15e3 100644 --- a/gcc/rust/backend/rust-compile-context.h +++ b/gcc/rust/backend/rust-compile-context.h @@ -28,6 +28,7 @@ #include "rust-ast-full.h" #include "rust-hir-full.h" #include "rust-hir-const-fold-ctx.h" +#include "rust-mangle.h" namespace Rust { namespace Compile { @@ -45,7 +46,7 @@ public: : backend (backend), resolver (Resolver::Resolver::get ()), tyctx (Resolver::TypeCheckContext::get ()), mappings (Analysis::Mappings::get ()), - const_ctx (ConstFold::Context::get ()) + const_ctx (ConstFold::Context::get ()), mangler (Mangler ()) { // insert the builtins auto builtins = resolver->get_builtin_types (); @@ -285,13 +286,19 @@ public: return pop; } - // this needs to support Legacy and V0 see github #429 or #305 std::string mangle_item (const TyTy::BaseType *ty, - const Resolver::CanonicalPath &path) const; + const Resolver::CanonicalPath &path) const + { + return mangler.mangle_item (ty, path, mappings->get_current_crate_name ()); + } std::string mangle_impl_item (const TyTy::BaseType *self, const TyTy::BaseType *ty, - const std::string &name) const; + const std::string &name) const + { + return mangler.mangle_impl_item (self, ty, name, + mappings->get_current_crate_name ()); + } private: ::Backend *backend; @@ -300,6 +307,7 @@ private: Analysis::Mappings *mappings; ConstFold::Context *const_ctx; std::set<HirId> builtin_range; + Mangler mangler; // state std::vector<fncontext> fn_stack; diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc index 0a65b15..ef2c16a 100644 --- a/gcc/rust/backend/rust-compile.cc +++ b/gcc/rust/backend/rust-compile.cc @@ -22,7 +22,6 @@ #include "rust-compile-struct-field-expr.h" #include "rust-hir-trait-resolve.h" #include "rust-hir-path-probe.h" -#include "fnv-hash.h" namespace Rust { namespace Compile { @@ -538,107 +537,5 @@ HIRCompileBase::compile_locals_for_block (Resolver::Rib &rib, Bfunction *fndecl, return true; } - -// 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 -mangle_canonical_path (const Resolver::CanonicalPath &path) -{ - std::string buffer; - path.iterate_segs ([&] (const Resolver::CanonicalPath &p) -> bool { - buffer += mangle_name (p.get ()); - return true; - }); - return buffer; -} - -// rustc uses a sip128 hash for legacy mangling, but an fnv 128 was quicker to -// implement for now -static std::string -legacy_hash (const std::string &fingerprint) -{ - Hash::FNV128 hasher; - hasher.write ((const unsigned char *) fingerprint.c_str (), - fingerprint.size ()); - - uint64_t hi, lo; - hasher.sum (&hi, &lo); - - char hex[16 + 1]; - memset (hex, 0, sizeof hex); - snprintf (hex, sizeof hex, "%08" PRIx64 "%08" PRIx64, lo, hi); - - return "h" + std::string (hex, sizeof (hex) - 1); -} - -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 TyTy::BaseType *ty, - const Resolver::CanonicalPath &path) const -{ - const std::string &crate_name = mappings->get_current_crate_name (); - - const std::string hash = legacy_hash (ty->as_string ()); - const std::string hash_sig = mangle_name (hash); - - return kMangledSymbolPrefix + mangle_name (crate_name) - + mangle_canonical_path (path) + hash_sig + kMangledSymbolDelim; -} - -// FIXME this is a wee bit broken -std::string -Context::mangle_impl_item (const TyTy::BaseType *self, const TyTy::BaseType *ty, - const std::string &name) const -{ - const std::string &crate_name = mappings->get_current_crate_name (); - - const std::string hash = legacy_hash (ty->as_string ()); - const std::string hash_sig = mangle_name (hash); - - return kMangledSymbolPrefix + mangle_name (crate_name) + mangle_self (self) - + mangle_name (name) + hash_sig + kMangledSymbolDelim; -} - } // namespace Compile } // namespace Rust diff --git a/gcc/rust/backend/rust-mangle.cc b/gcc/rust/backend/rust-mangle.cc new file mode 100644 index 0000000..40822b4 --- /dev/null +++ b/gcc/rust/backend/rust-mangle.cc @@ -0,0 +1,152 @@ +#include "rust-mangle.h" +#include "fnv-hash.h" + +// FIXME: Rename those to legacy_* +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$"; + +namespace Rust { +namespace Compile { + +Mangler::MangleVersion Mangler::version = MangleVersion::LEGACY; + +static std::string +legacy_mangle_name (const std::string &name) +{ + return std::to_string (name.size ()) + name; +} + +static std::string +legacy_mangle_canonical_path (const Resolver::CanonicalPath &path) +{ + std::string buffer; + path.iterate_segs ([&] (const Resolver::CanonicalPath &p) -> bool { + buffer += legacy_mangle_name (p.get ()); + return true; + }); + return buffer; +} + +// rustc uses a sip128 hash for legacy mangling, but an fnv 128 was quicker to +// implement for now +static std::string +legacy_hash (const std::string &fingerprint) +{ + Hash::FNV128 hasher; + hasher.write ((const unsigned char *) fingerprint.c_str (), + fingerprint.size ()); + + uint64_t hi, lo; + hasher.sum (&hi, &lo); + + char hex[16 + 1]; + memset (hex, 0, sizeof hex); + snprintf (hex, sizeof hex, "%08" PRIx64 "%08" PRIx64, lo, hi); + + return "h" + std::string (hex, sizeof (hex) - 1); +} + +static std::string +legacy_mangle_self (const TyTy::BaseType *self) +{ + if (self->get_kind () != TyTy::TypeKind::ADT) + return legacy_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 legacy_mangle_name (buf); +} + +static std::string +legacy_mangle_item (const TyTy::BaseType *ty, + const Resolver::CanonicalPath &path, + const std::string &crate_name) +{ + const std::string hash = legacy_hash (ty->as_string ()); + const std::string hash_sig = legacy_mangle_name (hash); + + return kMangledSymbolPrefix + legacy_mangle_name (crate_name) + + legacy_mangle_canonical_path (path) + hash_sig + kMangledSymbolDelim; +} + +// FIXME this is a wee bit broken +static std::string +legacy_mangle_impl_item (const TyTy::BaseType *self, const TyTy::BaseType *ty, + const std::string &name, const std::string &crate_name) +{ + const std::string hash = legacy_hash (ty->as_string ()); + const std::string hash_sig = legacy_mangle_name (hash); + + return kMangledSymbolPrefix + legacy_mangle_name (crate_name) + + legacy_mangle_self (self) + legacy_mangle_name (name) + hash_sig + + kMangledSymbolDelim; +} + +// FIXME: Uncomment once v0 mangling is implemented +// static std::string +// Mangler::v0_mangle_item (const TyTy::BaseType *ty, +// const std::string &name) +// {} +// +// static std::string +// Mangler::v0_mangle_impl_item (const TyTy::BaseType *self, +// const TyTy::BaseType *ty, +// const std::string &name) +// {} + +std::string +Mangler::mangle_item (const TyTy::BaseType *ty, + const Resolver::CanonicalPath &path, + const std::string &crate_name) const +{ + switch (version) + { + case Mangler::MangleVersion::LEGACY: + return legacy_mangle_item (ty, path, crate_name); + case Mangler::MangleVersion::V0: + gcc_unreachable (); + default: + gcc_unreachable (); + } +} + +std::string +Mangler::mangle_impl_item (const TyTy::BaseType *self, const TyTy::BaseType *ty, + const std::string &name, + const std::string &crate_name) const +{ + switch (version) + { + case Mangler::MangleVersion::LEGACY: + return legacy_mangle_impl_item (self, ty, name, crate_name); + case Mangler::MangleVersion::V0: + gcc_unreachable (); + default: + gcc_unreachable (); + } +} + +} // namespace Compile +} // namespace Rust diff --git a/gcc/rust/backend/rust-mangle.h b/gcc/rust/backend/rust-mangle.h new file mode 100644 index 0000000..9e77c54 --- /dev/null +++ b/gcc/rust/backend/rust-mangle.h @@ -0,0 +1,54 @@ +// This file is part of GCC. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// You should have received a copy of the GNU General Public License +// along with GCC; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#ifndef RUST_MANGLE_H +#define RUST_MANGLE_H + +#include "rust-compile-tyty.h" + +namespace Rust { +namespace Compile { +class Mangler +{ +public: + enum MangleVersion + { + // Values defined in rust/lang.opt + LEGACY = 0, + V0 = 1, + }; + + // this needs to support Legacy and V0 see github #429 or #305 + std::string mangle_item (const TyTy::BaseType *ty, + const Resolver::CanonicalPath &path, + const std::string &crate_name) const; + + std::string mangle_impl_item (const TyTy::BaseType *self, + const TyTy::BaseType *ty, + const std::string &name, + const std::string &crate_name) const; + + static void set_mangling (int frust_mangling_value) + { + version = static_cast<MangleVersion> (frust_mangling_value); + } + +private: + static enum MangleVersion version; +}; +} // namespace Compile +} // namespace Rust +#endif // RUST_MANGLE_H |