db9b5e59c6abec3ccdf5aacaab9d520aca4ba672
[oota-llvm.git] / 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/Twine.h"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/Support/raw_ostream.h"
21 using namespace llvm;
22
23 static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
24                                   Mangler::ManglerPrefixTy PrefixTy,
25                                   const DataLayout &DL, char Prefix) {
26   SmallString<256> TmpData;
27   StringRef Name = GVName.toStringRef(TmpData);
28   assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
29
30   // No need to do anything special if the global has the special "do not
31   // mangle" flag in the name.
32   if (Name[0] == '\1') {
33     OS << Name.substr(1);
34     return;
35   }
36
37   if (PrefixTy == Mangler::Private)
38     OS << DL.getPrivateGlobalPrefix();
39   else if (PrefixTy == Mangler::LinkerPrivate)
40     OS << DL.getLinkerPrivateGlobalPrefix();
41
42   if (Prefix != '\0')
43     OS << Prefix;
44
45   // If this is a simple string that doesn't need escaping, just append it.
46   OS << Name;
47 }
48
49 void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
50                                 ManglerPrefixTy PrefixTy) const {
51   char Prefix = DL->getGlobalPrefix();
52   return getNameWithPrefixImpl(OS, GVName, PrefixTy, *DL, Prefix);
53 }
54
55 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
56                                 const Twine &GVName, const DataLayout &DL) {
57   raw_svector_ostream OS(OutName);
58   char Prefix = DL.getGlobalPrefix();
59   return getNameWithPrefixImpl(OS, GVName, Mangler::Default, DL, Prefix);
60 }
61
62 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
63                                 const Twine &GVName,
64                                 ManglerPrefixTy PrefixTy) const {
65   raw_svector_ostream OS(OutName);
66   char Prefix = DL->getGlobalPrefix();
67   return getNameWithPrefixImpl(OS, GVName, PrefixTy, *DL, Prefix);
68 }
69
70 static bool hasByteCountSuffix(CallingConv::ID CC) {
71   switch (CC) {
72   case CallingConv::X86_FastCall:
73   case CallingConv::X86_StdCall:
74   case CallingConv::X86_VectorCall:
75     return true;
76   default:
77     return false;
78   }
79 }
80
81 /// Microsoft fastcall and stdcall functions require a suffix on their name
82 /// indicating the number of words of arguments they take.
83 static void addByteCountSuffix(raw_ostream &OS, const Function *F,
84                                const DataLayout &DL) {
85   // Calculate arguments size total.
86   unsigned ArgWords = 0;
87   for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
88        AI != AE; ++AI) {
89     Type *Ty = AI->getType();
90     // 'Dereference' type in case of byval or inalloca parameter attribute.
91     if (AI->hasByValOrInAllocaAttr())
92       Ty = cast<PointerType>(Ty)->getElementType();
93     // Size should be aligned to pointer size.
94     unsigned PtrSize = DL.getPointerSize();
95     ArgWords += RoundUpToAlignment(DL.getTypeAllocSize(Ty), PtrSize);
96   }
97
98   OS << '@' << ArgWords;
99 }
100
101 void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
102                                 bool CannotUsePrivateLabel) const {
103   ManglerPrefixTy PrefixTy = Mangler::Default;
104   if (GV->hasPrivateLinkage()) {
105     if (CannotUsePrivateLabel)
106       PrefixTy = Mangler::LinkerPrivate;
107     else
108       PrefixTy = Mangler::Private;
109   }
110
111   if (!GV->hasName()) {
112     // Get the ID for the global, assigning a new one if we haven't got one
113     // already.
114     unsigned &ID = AnonGlobalIDs[GV];
115     if (ID == 0)
116       ID = NextAnonGlobalID++;
117
118     // Must mangle the global into a unique ID.
119     getNameWithPrefix(OS, "__unnamed_" + Twine(ID), PrefixTy);
120     return;
121   }
122
123   StringRef Name = GV->getName();
124   char Prefix = DL->getGlobalPrefix();
125
126   // Mangle functions with Microsoft calling conventions specially.  Only do
127   // this mangling for x86_64 vectorcall and 32-bit x86.
128   const Function *MSFunc = dyn_cast<Function>(GV);
129   if (Name.startswith("\01"))
130     MSFunc = nullptr; // Don't mangle when \01 is present.
131   CallingConv::ID CC =
132       MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C;
133   if (!DL->hasMicrosoftFastStdCallMangling() &&
134       CC != CallingConv::X86_VectorCall)
135     MSFunc = nullptr;
136   if (MSFunc) {
137     if (CC == CallingConv::X86_FastCall)
138       Prefix = '@'; // fastcall functions have an @ prefix instead of _.
139     else if (CC == CallingConv::X86_VectorCall)
140       Prefix = '\0'; // vectorcall functions have no prefix.
141   }
142
143   getNameWithPrefixImpl(OS, Name, PrefixTy, *DL, Prefix);
144
145   if (!MSFunc)
146     return;
147
148   // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
149   // or vectorcall, add it.  These functions have a suffix of @N where N is the
150   // cumulative byte size of all of the parameters to the function in decimal.
151   if (CC == CallingConv::X86_VectorCall)
152     OS << '@'; // vectorcall functions use a double @ suffix.
153   FunctionType *FT = MSFunc->getFunctionType();
154   if (hasByteCountSuffix(CC) &&
155       // "Pure" variadic functions do not receive @0 suffix.
156       (!FT->isVarArg() || FT->getNumParams() == 0 ||
157        (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
158     addByteCountSuffix(OS, MSFunc, *DL);
159 }
160
161 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
162                                 const GlobalValue *GV,
163                                 bool CannotUsePrivateLabel) const {
164   raw_svector_ostream OS(OutName);
165   getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
166 }