]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/IR/Mangler.cpp
MFV r330591: 8984 fix for 6764 breaks ACL inheritance
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / IR / Mangler.cpp
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"
22 #include "llvm/Support/raw_ostream.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 (PrefixTy == Private)
48     OS << DL.getPrivateGlobalPrefix();
49   else if (PrefixTy == LinkerPrivate)
50     OS << DL.getLinkerPrivateGlobalPrefix();
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
66 void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
67                                 const DataLayout &DL) {
68   return getNameWithPrefixImpl(OS, GVName, DL, Default);
69 }
70
71 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
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
78 static bool hasByteCountSuffix(CallingConv::ID CC) {
79   switch (CC) {
80   case CallingConv::X86_FastCall:
81   case CallingConv::X86_StdCall:
82   case CallingConv::X86_VectorCall:
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
109 void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
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;
142   if (!DL.hasMicrosoftFastStdCallMangling() &&
143       CC != CallingConv::X86_VectorCall)
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
170 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
171                                 const GlobalValue *GV,
172                                 bool CannotUsePrivateLabel) const {
173   raw_svector_ostream OS(OutName);
174   getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
175 }
176
177 void llvm::emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV,
178                                         const Triple &TT, Mangler &Mangler) {
179   if (!GV->hasDLLExportStorageClass() || GV->isDeclaration())
180     return;
181
182   if (TT.isKnownWindowsMSVCEnvironment())
183     OS << " /EXPORT:";
184   else
185     OS << " -export:";
186
187   if (TT.isWindowsGNUEnvironment() || TT.isWindowsCygwinEnvironment()) {
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()) {
201     if (TT.isKnownWindowsMSVCEnvironment())
202       OS << ",DATA";
203     else
204       OS << ",data";
205   }
206 }