LLVM  13.0.0git
Mangler.cpp
Go to the documentation of this file.
1 //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
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 // Unified name mangler for assembly backends.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/IR/Mangler.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/Module.h"
23 using namespace llvm;
24 
25 namespace {
26 enum ManglerPrefixTy {
27  Default, ///< Emit default string before each symbol.
28  Private, ///< Emit "private" prefix before each symbol.
29  LinkerPrivate ///< Emit "linker private" prefix before each symbol.
30 };
31 }
32 
33 static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
34  ManglerPrefixTy PrefixTy,
35  const DataLayout &DL, char Prefix) {
36  SmallString<256> TmpData;
37  StringRef Name = GVName.toStringRef(TmpData);
38  assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
39 
40  // No need to do anything special if the global has the special "do not
41  // mangle" flag in the name.
42  if (Name[0] == '\1') {
43  OS << Name.substr(1);
44  return;
45  }
46 
47  if (DL.doNotMangleLeadingQuestionMark() && Name[0] == '?')
48  Prefix = '\0';
49 
50  if (PrefixTy == Private)
51  OS << DL.getPrivateGlobalPrefix();
52  else if (PrefixTy == LinkerPrivate)
53  OS << DL.getLinkerPrivateGlobalPrefix();
54 
55  if (Prefix != '\0')
56  OS << Prefix;
57 
58  // If this is a simple string that doesn't need escaping, just append it.
59  OS << Name;
60 }
61 
62 static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
63  const DataLayout &DL,
64  ManglerPrefixTy PrefixTy) {
65  char Prefix = DL.getGlobalPrefix();
66  return getNameWithPrefixImpl(OS, GVName, PrefixTy, DL, Prefix);
67 }
68 
70  const DataLayout &DL) {
71  return getNameWithPrefixImpl(OS, GVName, DL, Default);
72 }
73 
75  const Twine &GVName, const DataLayout &DL) {
76  raw_svector_ostream OS(OutName);
77  char Prefix = DL.getGlobalPrefix();
78  return getNameWithPrefixImpl(OS, GVName, Default, DL, Prefix);
79 }
80 
82  switch (CC) {
86  return true;
87  default:
88  return false;
89  }
90 }
91 
92 /// Microsoft fastcall and stdcall functions require a suffix on their name
93 /// indicating the number of words of arguments they take.
94 static void addByteCountSuffix(raw_ostream &OS, const Function *F,
95  const DataLayout &DL) {
96  // Calculate arguments size total.
97  unsigned ArgWords = 0;
98 
99  const unsigned PtrSize = DL.getPointerSize();
100 
101  for (const Argument &A : F->args()) {
102  // 'Dereference' type in case of byval or inalloca parameter attribute.
103  uint64_t AllocSize = A.hasPassPointeeByValueCopyAttr() ?
104  A.getPassPointeeByValueCopySize(DL) :
105  DL.getTypeAllocSize(A.getType());
106 
107  // Size should be aligned to pointer size.
108  ArgWords += alignTo(AllocSize, PtrSize);
109  }
110 
111  OS << '@' << ArgWords;
112 }
113 
115  bool CannotUsePrivateLabel) const {
116  ManglerPrefixTy PrefixTy = Default;
117  if (GV->hasPrivateLinkage()) {
118  if (CannotUsePrivateLabel)
119  PrefixTy = LinkerPrivate;
120  else
121  PrefixTy = Private;
122  }
123 
124  const DataLayout &DL = GV->getParent()->getDataLayout();
125  if (!GV->hasName()) {
126  // Get the ID for the global, assigning a new one if we haven't got one
127  // already.
128  unsigned &ID = AnonGlobalIDs[GV];
129  if (ID == 0)
130  ID = AnonGlobalIDs.size();
131 
132  // Must mangle the global into a unique ID.
133  getNameWithPrefixImpl(OS, "__unnamed_" + Twine(ID), DL, PrefixTy);
134  return;
135  }
136 
137  StringRef Name = GV->getName();
138  char Prefix = DL.getGlobalPrefix();
139 
140  // Mangle functions with Microsoft calling conventions specially. Only do
141  // this mangling for x86_64 vectorcall and 32-bit x86.
142  const Function *MSFunc = dyn_cast<Function>(GV);
143 
144  // Don't add byte count suffixes when '\01' or '?' are in the first
145  // character.
146  if (Name.startswith("\01") ||
147  (DL.doNotMangleLeadingQuestionMark() && Name.startswith("?")))
148  MSFunc = nullptr;
149 
150  CallingConv::ID CC =
151  MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C;
152  if (!DL.hasMicrosoftFastStdCallMangling() &&
154  MSFunc = nullptr;
155  if (MSFunc) {
156  if (CC == CallingConv::X86_FastCall)
157  Prefix = '@'; // fastcall functions have an @ prefix instead of _.
158  else if (CC == CallingConv::X86_VectorCall)
159  Prefix = '\0'; // vectorcall functions have no prefix.
160  }
161 
162  getNameWithPrefixImpl(OS, Name, PrefixTy, DL, Prefix);
163 
164  if (!MSFunc)
165  return;
166 
167  // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
168  // or vectorcall, add it. These functions have a suffix of @N where N is the
169  // cumulative byte size of all of the parameters to the function in decimal.
170  if (CC == CallingConv::X86_VectorCall)
171  OS << '@'; // vectorcall functions use a double @ suffix.
172  FunctionType *FT = MSFunc->getFunctionType();
173  if (hasByteCountSuffix(CC) &&
174  // "Pure" variadic functions do not receive @0 suffix.
175  (!FT->isVarArg() || FT->getNumParams() == 0 ||
176  (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
177  addByteCountSuffix(OS, MSFunc, DL);
178 }
179 
181  const GlobalValue *GV,
182  bool CannotUsePrivateLabel) const {
183  raw_svector_ostream OS(OutName);
184  getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
185 }
186 
187 // Check if the name needs quotes to be safe for the linker to interpret.
188 static bool canBeUnquotedInDirective(char C) {
189  return isAlnum(C) || C == '_' || C == '$' || C == '.' || C == '@';
190 }
191 
193  if (Name.empty())
194  return false;
195 
196  // If any of the characters in the string is an unacceptable character, force
197  // quotes.
198  for (char C : Name) {
200  return false;
201  }
202 
203  return true;
204 }
205 
207  const Triple &TT, Mangler &Mangler) {
208  if (!GV->hasDLLExportStorageClass() || GV->isDeclaration())
209  return;
210 
211  if (TT.isWindowsMSVCEnvironment())
212  OS << " /EXPORT:";
213  else
214  OS << " -export:";
215 
216  bool NeedQuotes = GV->hasName() && !canBeUnquotedInDirective(GV->getName());
217  if (NeedQuotes)
218  OS << "\"";
219  if (TT.isWindowsGNUEnvironment() || TT.isWindowsCygwinEnvironment()) {
220  std::string Flag;
221  raw_string_ostream FlagOS(Flag);
222  Mangler.getNameWithPrefix(FlagOS, GV, false);
223  FlagOS.flush();
224  if (Flag[0] == GV->getParent()->getDataLayout().getGlobalPrefix())
225  OS << Flag.substr(1);
226  else
227  OS << Flag;
228  } else {
229  Mangler.getNameWithPrefix(OS, GV, false);
230  }
231  if (NeedQuotes)
232  OS << "\"";
233 
234  if (!GV->getValueType()->isFunctionTy()) {
235  if (TT.isWindowsMSVCEnvironment())
236  OS << ",DATA";
237  else
238  OS << ",data";
239  }
240 }
241 
243  const Triple &T, Mangler &M) {
244  if (!T.isWindowsMSVCEnvironment())
245  return;
246 
247  OS << " /INCLUDE:";
248  bool NeedQuotes = GV->hasName() && !canBeUnquotedInDirective(GV->getName());
249  if (NeedQuotes)
250  OS << "\"";
251  M.getNameWithPrefix(OS, GV, false);
252  if (NeedQuotes)
253  OS << "\"";
254 }
255 
llvm::alignTo
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:158
llvm::Argument
This class represents an incoming formal argument to a Function.
Definition: Argument.h:29
llvm
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:112
llvm::cl::Prefix
@ Prefix
Definition: CommandLine.h:161
llvm::Function
Definition: Function.h:61
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:614
llvm::Value::hasName
bool hasName() const
Definition: Value.h:262
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
Module.h
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::FunctionType::getNumParams
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:138
llvm::Mangler
Definition: Mangler.h:27
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::FunctionType::isVarArg
bool isVarArg() const
Definition: DerivedTypes.h:122
llvm::CallingConv::X86_StdCall
@ X86_StdCall
X86_StdCall - stdcall is the calling conventions mostly used by the Win32 API.
Definition: CallingConv.h:97
llvm::codeview::MemberAccess::Private
@ Private
llvm::GlobalValue::isDeclaration
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:228
hasByteCountSuffix
static bool hasByteCountSuffix(CallingConv::ID CC)
Definition: Mangler.cpp:81
SmallString.h
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
Twine.h
canBeUnquotedInDirective
static bool canBeUnquotedInDirective(char C)
Definition: Mangler.cpp:188
llvm::GlobalValue::hasPrivateLinkage
bool hasPrivateLinkage() const
Definition: GlobalValue.h:444
llvm::Mangler::getNameWithPrefix
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Definition: Mangler.cpp:114
llvm::MCID::Flag
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:147
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:50
llvm::raw_ostream::flush
void flush()
Definition: raw_ostream.h:183
llvm::CallingConv::X86_VectorCall
@ X86_VectorCall
MSVC calling convention that passes vectors and vector aggregates in SSE registers.
Definition: CallingConv.h:168
llvm::Type::isFunctionTy
bool isFunctionTy() const
True if this is an instance of FunctionType.
Definition: Type.h:220
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
llvm::SmallString< 256 >
llvm::GlobalValue
Definition: GlobalValue.h:44
llvm::Function::getCallingConv
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:228
llvm::GlobalValue::getParent
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:572
StringExtras.h
llvm::Function::hasStructRetAttr
bool hasStructRetAttr() const
Determine if the function returns a structure through first or second pointer argument.
Definition: Function.h:684
llvm::DataLayout::getGlobalPrefix
char getGlobalPrefix() const
Definition: DataLayout.h:315
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
addByteCountSuffix
static void addByteCountSuffix(raw_ostream &OS, const Function *F, const DataLayout &DL)
Microsoft fastcall and stdcall functions require a suffix on their name indicating the number of word...
Definition: Mangler.cpp:94
Mangler.h
Triple.h
DataLayout.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::emitLinkerFlagsForUsedCOFF
void emitLinkerFlagsForUsedCOFF(raw_ostream &OS, const GlobalValue *GV, const Triple &T, Mangler &M)
Definition: Mangler.cpp:242
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:298
getNameWithPrefixImpl
static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName, ManglerPrefixTy PrefixTy, const DataLayout &DL, char Prefix)
Definition: Mangler.cpp:33
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::CallingConv::C
@ C
C - The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
llvm::GlobalValue::hasDLLExportStorageClass
bool hasDLLExportStorageClass() const
Definition: GlobalValue.h:262
Function.h
llvm::TargetStackID::Default
@ Default
Definition: TargetFrameLowering.h:28
llvm::Function::getFunctionType
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.h:165
llvm::emitLinkerFlagsForGlobalCOFF
void emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV, const Triple &TT, Mangler &Mangler)
Definition: Mangler.cpp:206
llvm::raw_svector_ostream
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:647
llvm::SmallVectorImpl< char >
llvm::Module::getDataLayout
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:397
DerivedTypes.h
llvm::GlobalValue::getValueType
Type * getValueType() const
Definition: GlobalValue.h:273
llvm::Twine::toStringRef
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition: Twine.h:455
llvm::CallingConv::X86_FastCall
@ X86_FastCall
X86_FastCall - 'fast' analog of X86_StdCall.
Definition: CallingConv.h:102
raw_ostream.h
llvm::FunctionType
Class to represent function types.
Definition: DerivedTypes.h:102
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38