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