LLVM  6.0.0svn
Mangler.cpp
Go to the documentation of this file.
1 //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Unified name mangler for assembly backends.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/IR/Mangler.h"
15 #include "llvm/ADT/SmallString.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 {
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 (PrefixTy == Private)
48  OS << DL.getPrivateGlobalPrefix();
49  else if (PrefixTy == LinkerPrivate)
51 
52  if (Prefix != '\0')
53  OS << Prefix;
54 
55  // If this is a simple string that doesn't need escaping, just append it.
56  OS << Name;
57 }
58 
59 static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
60  const DataLayout &DL,
61  ManglerPrefixTy PrefixTy) {
62  char Prefix = DL.getGlobalPrefix();
63  return getNameWithPrefixImpl(OS, GVName, PrefixTy, DL, Prefix);
64 }
65 
67  const DataLayout &DL) {
68  return getNameWithPrefixImpl(OS, GVName, DL, Default);
69 }
70 
72  const Twine &GVName, const DataLayout &DL) {
73  raw_svector_ostream OS(OutName);
74  char Prefix = DL.getGlobalPrefix();
75  return getNameWithPrefixImpl(OS, GVName, Default, DL, Prefix);
76 }
77 
79  switch (CC) {
83  return true;
84  default:
85  return false;
86  }
87 }
88 
89 /// Microsoft fastcall and stdcall functions require a suffix on their name
90 /// indicating the number of words of arguments they take.
91 static void addByteCountSuffix(raw_ostream &OS, const Function *F,
92  const DataLayout &DL) {
93  // Calculate arguments size total.
94  unsigned ArgWords = 0;
95  for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
96  AI != AE; ++AI) {
97  Type *Ty = AI->getType();
98  // 'Dereference' type in case of byval or inalloca parameter attribute.
99  if (AI->hasByValOrInAllocaAttr())
100  Ty = cast<PointerType>(Ty)->getElementType();
101  // Size should be aligned to pointer size.
102  unsigned PtrSize = DL.getPointerSize();
103  ArgWords += alignTo(DL.getTypeAllocSize(Ty), PtrSize);
104  }
105 
106  OS << '@' << ArgWords;
107 }
108 
110  bool CannotUsePrivateLabel) const {
111  ManglerPrefixTy PrefixTy = Default;
112  if (GV->hasPrivateLinkage()) {
113  if (CannotUsePrivateLabel)
114  PrefixTy = LinkerPrivate;
115  else
116  PrefixTy = Private;
117  }
118 
119  const DataLayout &DL = GV->getParent()->getDataLayout();
120  if (!GV->hasName()) {
121  // Get the ID for the global, assigning a new one if we haven't got one
122  // already.
123  unsigned &ID = AnonGlobalIDs[GV];
124  if (ID == 0)
125  ID = AnonGlobalIDs.size();
126 
127  // Must mangle the global into a unique ID.
128  getNameWithPrefixImpl(OS, "__unnamed_" + Twine(ID), DL, PrefixTy);
129  return;
130  }
131 
132  StringRef Name = GV->getName();
133  char Prefix = DL.getGlobalPrefix();
134 
135  // Mangle functions with Microsoft calling conventions specially. Only do
136  // this mangling for x86_64 vectorcall and 32-bit x86.
137  const Function *MSFunc = dyn_cast<Function>(GV);
138  if (Name.startswith("\01"))
139  MSFunc = nullptr; // Don't mangle when \01 is present.
140  CallingConv::ID CC =
141  MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C;
144  MSFunc = nullptr;
145  if (MSFunc) {
146  if (CC == CallingConv::X86_FastCall)
147  Prefix = '@'; // fastcall functions have an @ prefix instead of _.
148  else if (CC == CallingConv::X86_VectorCall)
149  Prefix = '\0'; // vectorcall functions have no prefix.
150  }
151 
152  getNameWithPrefixImpl(OS, Name, PrefixTy, DL, Prefix);
153 
154  if (!MSFunc)
155  return;
156 
157  // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
158  // or vectorcall, add it. These functions have a suffix of @N where N is the
159  // cumulative byte size of all of the parameters to the function in decimal.
160  if (CC == CallingConv::X86_VectorCall)
161  OS << '@'; // vectorcall functions use a double @ suffix.
162  FunctionType *FT = MSFunc->getFunctionType();
163  if (hasByteCountSuffix(CC) &&
164  // "Pure" variadic functions do not receive @0 suffix.
165  (!FT->isVarArg() || FT->getNumParams() == 0 ||
166  (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
167  addByteCountSuffix(OS, MSFunc, DL);
168 }
169 
171  const GlobalValue *GV,
172  bool CannotUsePrivateLabel) const {
173  raw_svector_ostream OS(OutName);
174  getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
175 }
176 
178  const Triple &TT, Mangler &Mangler) {
179  if (!GV->hasDLLExportStorageClass() || GV->isDeclaration())
180  return;
181 
183  OS << " /EXPORT:";
184  else
185  OS << " -export:";
186 
188  std::string Flag;
189  raw_string_ostream FlagOS(Flag);
190  Mangler.getNameWithPrefix(FlagOS, GV, false);
191  FlagOS.flush();
192  if (Flag[0] == GV->getParent()->getDataLayout().getGlobalPrefix())
193  OS << Flag.substr(1);
194  else
195  OS << Flag;
196  } else {
197  Mangler.getNameWithPrefix(OS, GV, false);
198  }
199 
200  if (!GV->getValueType()->isFunctionTy()) {
202  OS << ",DATA";
203  else
204  OS << ",data";
205  }
206 }
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:109
bool hasDLLExportStorageClass() const
Definition: GlobalValue.h:251
This class represents an incoming formal argument to a Function.
Definition: Argument.h:30
bool hasPrivateLinkage() const
Definition: GlobalValue.h:415
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
StringRef getPrivateGlobalPrefix() const
Definition: DataLayout.h:281
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:91
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition: Twine.h:463
MSVC calling convention that passes vectors and vector aggregates in SSE registers.
Definition: CallingConv.h:158
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:493
arg_iterator arg_end()
Definition: Function.h:612
F(f)
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:677
char getGlobalPrefix() const
Definition: DataLayout.h:267
bool isWindowsCygwinEnvironment() const
Definition: Triple.h:534
StringRef getLinkerPrivateGlobalPrefix() const
Definition: DataLayout.h:261
C - The default llvm calling convention, compatible with C.
Definition: CallingConv.h:35
bool hasStructRetAttr() const
Determine if the function returns a structure through first or second pointer argument.
Definition: Function.h:511
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:361
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
bool isKnownWindowsMSVCEnvironment() const
Checks if the environment is MSVC.
Definition: Triple.h:522
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:267
Class to represent function types.
Definition: DerivedTypes.h:103
X86_FastCall - &#39;fast&#39; analog of X86_StdCall.
Definition: CallingConv.h:92
bool isVarArg() const
Definition: DerivedTypes.h:123
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:598
bool isWindowsGNUEnvironment() const
Definition: Triple.h:538
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:121
bool hasName() const
Definition: Value.h:251
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
unsigned getPointerSize(unsigned AS=0) const
Layout pointer size FIXME: The defaults need to be removed once all of the backends/clients are updat...
Definition: DataLayout.cpp:605
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:139
void emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV, const Triple &TT, Mangler &Mangler)
Definition: Mangler.cpp:177
arg_iterator arg_begin()
Definition: Function.h:603
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
ManglerPrefixTy
Definition: Mangler.cpp:26
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:194
Module.h This file contains the declarations for the Module class.
X86_StdCall - stdcall is the calling conventions mostly used by the Win32 API.
Definition: CallingConv.h:87
bool hasMicrosoftFastStdCallMangling() const
Definition: DataLayout.h:255
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.h:145
static bool hasByteCountSuffix(CallingConv::ID CC)
Definition: Mangler.cpp:78
static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName, ManglerPrefixTy PrefixTy, const DataLayout &DL, char Prefix)
Definition: Mangler.cpp:33
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:405
bool isFunctionTy() const
True if this is an instance of FunctionType.
Definition: Type.h:212
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:218
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
Type * getValueType() const
Definition: GlobalValue.h:262
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:200
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:466
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:545
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable&#39;s name.
Definition: Mangler.cpp:109
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49