[IR][ARM64EC][NFC] Clean up and document ARM64EC mangling helpers. (#107230)

This commit is contained in:
Jacek Caban 2024-09-05 12:53:57 +02:00 committed by GitHub
parent 3299bc863f
commit 95684afbcd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 19 deletions

View File

@ -53,7 +53,12 @@ void emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV,
void emitLinkerFlagsForUsedCOFF(raw_ostream &OS, const GlobalValue *GV,
const Triple &T, Mangler &M);
/// Returns the ARM64EC mangled function name unless the input is already
/// mangled.
std::optional<std::string> getArm64ECMangledFunctionName(StringRef Name);
/// Returns the ARM64EC demangled function name, unless the input is not
/// mangled.
std::optional<std::string> getArm64ECDemangledFunctionName(StringRef Name);
} // End llvm namespace

View File

@ -291,39 +291,40 @@ void llvm::emitLinkerFlagsForUsedCOFF(raw_ostream &OS, const GlobalValue *GV,
}
std::optional<std::string> llvm::getArm64ECMangledFunctionName(StringRef Name) {
bool IsCppFn = Name[0] == '?';
if (IsCppFn && Name.contains("$$h"))
return std::nullopt;
if (!IsCppFn && Name[0] == '#')
return std::nullopt;
if (Name[0] != '?') {
// For non-C++ symbols, prefix the name with "#" unless it's already
// mangled.
if (Name[0] == '#')
return std::nullopt;
return std::optional<std::string>(("#" + Name).str());
}
StringRef Prefix = "$$h";
size_t InsertIdx = 0;
if (IsCppFn) {
InsertIdx = Name.find("@@");
size_t ThreeAtSignsIdx = Name.find("@@@");
if (InsertIdx != std::string::npos && InsertIdx != ThreeAtSignsIdx) {
InsertIdx += 2;
} else {
InsertIdx = Name.find("@");
if (InsertIdx != std::string::npos)
InsertIdx++;
}
// Insert the ARM64EC "$$h" tag after the mangled function name.
if (Name.contains("$$h"))
return std::nullopt;
size_t InsertIdx = Name.find("@@");
size_t ThreeAtSignsIdx = Name.find("@@@");
if (InsertIdx != std::string::npos && InsertIdx != ThreeAtSignsIdx) {
InsertIdx += 2;
} else {
Prefix = "#";
InsertIdx = Name.find("@");
if (InsertIdx != std::string::npos)
InsertIdx++;
}
return std::optional<std::string>(
(Name.substr(0, InsertIdx) + Prefix + Name.substr(InsertIdx)).str());
(Name.substr(0, InsertIdx) + "$$h" + Name.substr(InsertIdx)).str());
}
std::optional<std::string>
llvm::getArm64ECDemangledFunctionName(StringRef Name) {
// For non-C++ names, drop the "#" prefix.
if (Name[0] == '#')
return std::optional<std::string>(Name.substr(1));
if (Name[0] != '?')
return std::nullopt;
// Drop the ARM64EC "$$h" tag.
std::pair<StringRef, StringRef> Pair = Name.split("$$h");
if (Pair.second.empty())
return std::nullopt;