LLVM  3.7.0
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/Twine.h"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/Module.h"
22 using namespace llvm;
23 
24 namespace {
26  Default, ///< Emit default string before each symbol.
27  Private, ///< Emit "private" prefix before each symbol.
28  LinkerPrivate ///< Emit "linker private" prefix before each symbol.
29 };
30 }
31 
32 static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
33  ManglerPrefixTy PrefixTy,
34  const DataLayout &DL, char Prefix) {
35  SmallString<256> TmpData;
36  StringRef Name = GVName.toStringRef(TmpData);
37  assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
38 
39  // No need to do anything special if the global has the special "do not
40  // mangle" flag in the name.
41  if (Name[0] == '\1') {
42  OS << Name.substr(1);
43  return;
44  }
45 
46  if (PrefixTy == Private)
47  OS << DL.getPrivateGlobalPrefix();
48  else if (PrefixTy == LinkerPrivate)
50 
51  if (Prefix != '\0')
52  OS << Prefix;
53 
54  // If this is a simple string that doesn't need escaping, just append it.
55  OS << Name;
56 }
57 
58 static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
59  const DataLayout &DL,
60  ManglerPrefixTy PrefixTy) {
61  char Prefix = DL.getGlobalPrefix();
62  return getNameWithPrefixImpl(OS, GVName, PrefixTy, DL, Prefix);
63 }
64 
66  const DataLayout &DL) {
67  return getNameWithPrefixImpl(OS, GVName, DL, Default);
68 }
69 
71  const Twine &GVName, const DataLayout &DL) {
72  raw_svector_ostream OS(OutName);
73  char Prefix = DL.getGlobalPrefix();
74  return getNameWithPrefixImpl(OS, GVName, Default, DL, Prefix);
75 }
76 
78  switch (CC) {
82  return true;
83  default:
84  return false;
85  }
86 }
87 
88 /// Microsoft fastcall and stdcall functions require a suffix on their name
89 /// indicating the number of words of arguments they take.
90 static void addByteCountSuffix(raw_ostream &OS, const Function *F,
91  const DataLayout &DL) {
92  // Calculate arguments size total.
93  unsigned ArgWords = 0;
94  for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
95  AI != AE; ++AI) {
96  Type *Ty = AI->getType();
97  // 'Dereference' type in case of byval or inalloca parameter attribute.
98  if (AI->hasByValOrInAllocaAttr())
99  Ty = cast<PointerType>(Ty)->getElementType();
100  // Size should be aligned to pointer size.
101  unsigned PtrSize = DL.getPointerSize();
102  ArgWords += RoundUpToAlignment(DL.getTypeAllocSize(Ty), PtrSize);
103  }
104 
105  OS << '@' << ArgWords;
106 }
107 
109  bool CannotUsePrivateLabel) const {
110  ManglerPrefixTy PrefixTy = Default;
111  if (GV->hasPrivateLinkage()) {
112  if (CannotUsePrivateLabel)
113  PrefixTy = LinkerPrivate;
114  else
115  PrefixTy = Private;
116  }
117 
118  const DataLayout &DL = GV->getParent()->getDataLayout();
119  if (!GV->hasName()) {
120  // Get the ID for the global, assigning a new one if we haven't got one
121  // already.
122  unsigned &ID = AnonGlobalIDs[GV];
123  if (ID == 0)
124  ID = NextAnonGlobalID++;
125 
126  // Must mangle the global into a unique ID.
127  getNameWithPrefixImpl(OS, "__unnamed_" + Twine(ID), DL, PrefixTy);
128  return;
129  }
130 
131  StringRef Name = GV->getName();
132  char Prefix = DL.getGlobalPrefix();
133 
134  // Mangle functions with Microsoft calling conventions specially. Only do
135  // this mangling for x86_64 vectorcall and 32-bit x86.
136  const Function *MSFunc = dyn_cast<Function>(GV);
137  if (Name.startswith("\01"))
138  MSFunc = nullptr; // Don't mangle when \01 is present.
139  CallingConv::ID CC =
140  MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C;
143  MSFunc = nullptr;
144  if (MSFunc) {
145  if (CC == CallingConv::X86_FastCall)
146  Prefix = '@'; // fastcall functions have an @ prefix instead of _.
147  else if (CC == CallingConv::X86_VectorCall)
148  Prefix = '\0'; // vectorcall functions have no prefix.
149  }
150 
151  getNameWithPrefixImpl(OS, Name, PrefixTy, DL, Prefix);
152 
153  if (!MSFunc)
154  return;
155 
156  // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
157  // or vectorcall, add it. These functions have a suffix of @N where N is the
158  // cumulative byte size of all of the parameters to the function in decimal.
159  if (CC == CallingConv::X86_VectorCall)
160  OS << '@'; // vectorcall functions use a double @ suffix.
161  FunctionType *FT = MSFunc->getFunctionType();
162  if (hasByteCountSuffix(CC) &&
163  // "Pure" variadic functions do not receive @0 suffix.
164  (!FT->isVarArg() || FT->getNumParams() == 0 ||
165  (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
166  addByteCountSuffix(OS, MSFunc, DL);
167 }
168 
170  const GlobalValue *GV,
171  bool CannotUsePrivateLabel) const {
172  raw_svector_ostream OS(OutName);
173  getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
174 }
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
bool hasName() const
Definition: Value.h:228
unsigned getNumParams() const
getNumParams - Return the number of fixed parameters this function type requires. ...
Definition: DerivedTypes.h:136
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:90
const char * getPrivateGlobalPrefix() const
Definition: DataLayout.h:281
StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:405
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:488
arg_iterator arg_end()
Definition: Function.h:480
F(f)
char getGlobalPrefix() const
Definition: DataLayout.h:267
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:172
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:188
bool hasMicrosoftFastStdCallMangling() const
Definition: DataLayout.h:255
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
bool hasPrivateLinkage() const
Definition: GlobalValue.h:279
FunctionType - Class to represent function types.
Definition: DerivedTypes.h:96
bool hasStructRetAttr() const
Determine if the function returns a structure through first pointer argument.
Definition: Function.h:360
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
X86_StdCall - stdcall is the calling conventions mostly used by the Win32 API.
Definition: CallingConv.h:80
arg_iterator arg_begin()
Definition: Function.h:472
ManglerPrefixTy
Definition: Mangler.cpp:25
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:215
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:388
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition: Twine.h:454
Module.h This file contains the declarations for the Module class.
const char * getLinkerPrivateGlobalPrefix() const
Definition: DataLayout.h:261
X86_FastCall - 'fast' analog of X86_StdCall.
Definition: CallingConv.h:85
LLVM_ATTRIBUTE_UNUSED_RESULT 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:285
static bool hasByteCountSuffix(CallingConv::ID CC)
Definition: Mangler.cpp:77
uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:609
static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName, ManglerPrefixTy PrefixTy, const DataLayout &DL, char Prefix)
Definition: Mangler.cpp:32
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:372
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:108
MSVC calling convention that passes vectors and vector aggregates in SSE registers.
Definition: CallingConv.h:147
FunctionType * getFunctionType() const
Definition: Function.cpp:227
bool isVarArg() const
Definition: DerivedTypes.h:120
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:365
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
C - The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
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:593
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110