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
18#include "llvm/ADT/StringRef.h"
23
24namespace llvm {
25
26class Triple;
27
28namespace orc {
29
30/// Applies linker name-mangling for a target.
31class Mangler {
32public:
33 /// The linker name-mangling scheme for a target, determined by its object
34 /// format. This captures the platform decoration applied to symbol names
35 /// (e.g. a leading '_' on MachO), independently of any ExecutionSession.
37
38 explicit Mangler(Mode MM) : MM(MM) {}
39 explicit Mangler(StringRef DLStr) : MM(fromDataLayoutStr(DLStr)) {}
40 explicit Mangler(const Triple &TT, StringRef ABIName = "")
41 : MM(fromTriple(TT, ABIName)) {}
42
43 /// Calls the given callback with the mangled version of NameSpec as a
44 /// StringRef. The mangled name is only valid for the duration of the callback
45 /// and must not escape. This allows withMangledNameDo to avoid allocations
46 /// when mangling is a no-op.
47 template <typename HandlerFn>
48 decltype(auto) withMangledNameDo(HandlerFn &&H,
49 const SymbolNameSpec &NameSpec) const {
50 if (NameSpec.getKind() == SymbolNameKind::Verbatim ||
51 NameSpec.getKind() == SymbolNameKind::Linker)
52 return H(NameSpec.getName());
53
54 if (NameSpec.getName().empty())
55 return H(NameSpec.getName());
56
57 if (NameSpec.getName()[0] == '\1')
58 return H(NameSpec.getName().substr(1));
59
60 if (NameSpec.getName()[0] == '?' && doNotMangleLeadingQuestionMark())
61 return H(NameSpec.getName());
62
63 if (MM == Mode::MachO || MM == Mode::WinCOFFX86) {
64 SmallString<1024> MangledName;
65 MangledName.append({StringRef("_"), NameSpec.getName()});
66 return H(StringRef(MangledName));
67 }
68
69 return H(NameSpec.getName());
70 }
71
72 /// Construct a mangled version of the given name as a std::string.
73 /// This always produces a copy, even for no-op manglings. Prefer
74 /// withMangledNameDo in any performance-sensitive context.
75 std::string mangledCopy(const SymbolNameSpec &Name) const {
76 return withMangledNameDo(
77 [](StringRef MangledName) { return MangledName.str(); }, Name);
78 }
79
80private:
81 // TODO: Sink this back into a .cpp file once OrcJIT and OrcSupport have
82 // been reorganized.
83 static Mode fromDataLayoutStr(StringRef DLStr) {
84
85 for (StringRef Spec : split(DLStr, '-')) {
86 if (!Spec.starts_with("m:"))
87 continue;
88 auto ModeStr = Spec.drop_front(2);
89 assert(ModeStr.size() == 1 &&
90 "invalid data layout string from Triple::computeDataLayout");
91 switch (ModeStr[0]) {
92 case 'e':
93 return Mode::ELF;
94 case 'l':
95 return Mode::GOFF;
96 case 'o':
97 return Mode::MachO;
98 case 'm':
99 return Mode::Mips;
100 case 'w':
101 return Mode::WinCOFF;
102 case 'x':
103 return Mode::WinCOFFX86;
104 case 'a':
105 return Mode::XCOFF;
106 default:
108 "Invalid mangling mode from Triple::computeDataLayout");
109 }
110 }
111 return Mode::None;
112 }
113
114 // TODO: Sink this back into a .cpp file once OrcJIT and OrcSupport have
115 // been reorganized.
116 static Mode fromTriple(const Triple &TT, StringRef ABIName) {
117 return fromDataLayoutStr(TT.computeDataLayout(ABIName));
118 }
119
120 bool doNotMangleLeadingQuestionMark() const {
121 return MM == Mode::WinCOFF || MM == Mode::WinCOFFX86;
122 }
123
124 Mode MM;
125};
126
127} // namespace orc
128} // namespace llvm
129
130#endif // LLVM_EXECUTIONENGINE_ORC_SHARED_MANGLING_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#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.
This file contains some functions that are useful when dealing with strings.
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:36
Mangler(const Triple &TT, StringRef ABIName="")
Definition Mangler.h:40
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:48
Mangler(StringRef DLStr)
Definition Mangler.h:39
std::string mangledCopy(const SymbolNameSpec &Name) const
Construct a mangled version of the given name as a std::string.
Definition Mangler.h:75
Mangler(Mode MM)
Definition Mangler.h:38
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
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
iterator_range< SplittingIterator > split(StringRef Str, StringRef Separator)
Split the specified string over a separator and return a range-compatible iterable over its partition...