LLVM 24.0.0git
Mangler.h
Go to the documentation of this file.
1//===------- Mangler.h -- Linker name mangling for ORC ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Linker name mangling for ORC.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_EXECUTIONENGINE_ORC_SHARED_MANGLER_H
14#define LLVM_EXECUTIONENGINE_ORC_SHARED_MANGLER_H
15
17#include "llvm/ADT/StringRef.h"
20
21namespace llvm {
22
23class Triple;
24
25namespace orc {
26
27/// Applies linker name-mangling for a target.
28class Mangler {
29public:
30 /// The linker name-mangling scheme for a target, determined by its object
31 /// format. This captures the platform decoration applied to symbol names
32 /// (e.g. a leading '_' on MachO), independently of any ExecutionSession.
34
35 explicit Mangler(Mode MM) : MM(MM) {}
36 explicit Mangler(StringRef DLStr) : MM(fromDataLayoutStr(DLStr)) {}
37 explicit Mangler(const Triple &TT, StringRef ABIName = "")
38 : MM(fromTriple(TT, ABIName)) {}
39
40 /// Calls the given callback with the mangled version of NameSpec as a
41 /// StringRef. The mangled name is only valid for the duration of the callback
42 /// and must not escape. This allows withMangledNameDo to avoid allocations
43 /// when mangling is a no-op.
44 template <typename HandlerFn>
45 decltype(auto) withMangledNameDo(HandlerFn &&H,
46 const SymbolNameSpec &NameSpec) const {
47 if (NameSpec.getKind() == SymbolNameKind::Verbatim ||
48 NameSpec.getKind() == SymbolNameKind::Linker)
49 return H(NameSpec.getName());
50
51 if (NameSpec.getName().empty())
52 return H(NameSpec.getName());
53
54 if (NameSpec.getName()[0] == '\1')
55 return H(NameSpec.getName().substr(1));
56
57 if (NameSpec.getName()[0] == '?' && doNotMangleLeadingQuestionMark())
58 return H(NameSpec.getName());
59
60 if (MM == Mode::MachO || MM == Mode::WinCOFFX86) {
61 SmallString<1024> MangledName;
62 MangledName.append({StringRef("_"), NameSpec.getName()});
63 return H(StringRef(MangledName));
64 }
65
66 return H(NameSpec.getName());
67 }
68
69 /// Construct a mangled version of the given name as a std::string.
70 /// This always produces a copy, even for no-op manglings. Prefer
71 /// withMangledNameDo in any performance-sensitive context.
72 std::string mangledCopy(const SymbolNameSpec &Name) const {
73 return withMangledNameDo(
74 [](StringRef MangledName) { return MangledName.str(); }, Name);
75 }
76
77private:
78 LLVM_ABI static Mode fromDataLayoutStr(StringRef DLStr);
79 LLVM_ABI static Mode fromTriple(const Triple &TT, StringRef ABIName);
80 bool doNotMangleLeadingQuestionMark() const {
81 return MM == Mode::WinCOFF || MM == Mode::WinCOFFX86;
82 }
83
84 Mode MM;
85};
86
87} // namespace orc
88} // namespace llvm
89
90#endif // LLVM_EXECUTIONENGINE_ORC_SHARED_MANGLING_H
#define LLVM_ABI
Definition Compiler.h:215
#define H(x, y, z)
Definition MD5.cpp:56
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
This file defines the SmallString class.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
void append(StringRef RHS)
Append from a StringRef.
Definition SmallString.h:68
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
std::string str() const
Get the contents as an std::string.
Definition StringRef.h:222
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:597
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
Mode
The linker name-mangling scheme for a target, determined by its object format.
Definition Mangler.h:33
Mangler(const Triple &TT, StringRef ABIName="")
Definition Mangler.h:37
decltype(auto) withMangledNameDo(HandlerFn &&H, const SymbolNameSpec &NameSpec) const
Calls the given callback with the mangled version of NameSpec as a StringRef.
Definition Mangler.h:45
Mangler(StringRef DLStr)
Definition Mangler.h:36
std::string mangledCopy(const SymbolNameSpec &Name) const
Construct a mangled version of the given name as a std::string.
Definition Mangler.h:72
Mangler(Mode MM)
Definition Mangler.h:35
A symbol name together with the naming level (SymbolNameKind) it is expressed in, so that a Mangler /...
constexpr StringRef getName() const
constexpr SymbolNameKind getKind() const
This is an optimization pass for GlobalISel generic memory operations.