5eeb7978e2f3c369da86ff88f8e2e5e8c1200615
[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 getNameWithPrefixx(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 getNameWithPrefixx(OS, GVName, PrefixTy, *DL, Prefix);
53 }
54
55 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
56                                 const Twine &GVName,
57                                 ManglerPrefixTy PrefixTy) const {
58   raw_svector_ostream OS(OutName);
59   return getNameWithPrefix(OS, GVName, PrefixTy);
60 }
61
62 static bool hasByteCountSuffix(CallingConv::ID CC) {
63   switch (CC) {
64   case CallingConv::X86_FastCall:
65   case CallingConv::X86_StdCall:
66   case CallingConv::X86_VectorCall:
67     return true;
68   default:
69     return false;
70   }
71 }
72
73 /// Microsoft fastcall and stdcall functions require a suffix on their name
74 /// indicating the number of words of arguments they take.
75 static void addByteCountSuffix(raw_ostream &OS, const Function *F,
76                                const DataLayout &TD) {
77   // Calculate arguments size total.
78   unsigned ArgWords = 0;
79   for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
80        AI != AE; ++AI) {
81     Type *Ty = AI->getType();
82     // 'Dereference' type in case of byval or inalloca parameter attribute.
83     if (AI->hasByValOrInAllocaAttr())
84       Ty = cast<PointerType>(Ty)->getElementType();
85     // Size should be aligned to pointer size.
86     unsigned PtrSize = TD.getPointerSize();
87     ArgWords += RoundUpToAlignment(TD.getTypeAllocSize(Ty), PtrSize);
88   }
89
90   OS << '@' << ArgWords;
91 }
92
93 void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
94                                 bool CannotUsePrivateLabel) const {
95   ManglerPrefixTy PrefixTy = Mangler::Default;
96   if (GV->hasPrivateLinkage()) {
97     if (CannotUsePrivateLabel)
98       PrefixTy = Mangler::LinkerPrivate;
99     else
100       PrefixTy = Mangler::Private;
101   }
102
103   if (!GV->hasName()) {
104     // Get the ID for the global, assigning a new one if we haven't got one
105     // already.
106     unsigned &ID = AnonGlobalIDs[GV];
107     if (ID == 0)
108       ID = NextAnonGlobalID++;
109
110     // Must mangle the global into a unique ID.
111     getNameWithPrefix(OS, "__unnamed_" + Twine(ID), PrefixTy);
112     return;
113   }
114
115   StringRef Name = GV->getName();
116   char Prefix = DL->getGlobalPrefix();
117
118   // Mangle functions with Microsoft calling conventions specially.  Only do
119   // this mangling for x86_64 vectorcall and 32-bit x86.
120   const Function *MSFunc = dyn_cast<Function>(GV);
121   if (Name.startswith("\01"))
122     MSFunc = nullptr; // Don't mangle when \01 is present.
123   CallingConv::ID CC =
124       MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C;
125   if (!DL->hasMicrosoftFastStdCallMangling() &&
126       CC != CallingConv::X86_VectorCall)
127     MSFunc = nullptr;
128   if (MSFunc) {
129     if (CC == CallingConv::X86_FastCall)
130       Prefix = '@'; // fastcall functions have an @ prefix instead of _.
131     else if (CC == CallingConv::X86_VectorCall)
132       Prefix = '\0'; // vectorcall functions have no prefix.
133   }
134
135   getNameWithPrefixx(OS, Name, PrefixTy, *DL, Prefix);
136
137   if (!MSFunc)
138     return;
139
140   // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
141   // or vectorcall, add it.  These functions have a suffix of @N where N is the
142   // cumulative byte size of all of the parameters to the function in decimal.
143   if (CC == CallingConv::X86_VectorCall)
144     OS << '@'; // vectorcall functions use a double @ suffix.
145   FunctionType *FT = MSFunc->getFunctionType();
146   if (hasByteCountSuffix(CC) &&
147       // "Pure" variadic functions do not receive @0 suffix.
148       (!FT->isVarArg() || FT->getNumParams() == 0 ||
149        (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
150     addByteCountSuffix(OS, MSFunc, *DL);
151 }
152
153 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
154                                 const GlobalValue *GV,
155                                 bool CannotUsePrivateLabel) const {
156   raw_svector_ostream OS(OutName);
157   getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
158 }