1 //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Unified name mangler for assembly backends.
12 //===----------------------------------------------------------------------===//
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"
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.
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");
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') {
47 if (DL
.doNotMangleLeadingQuestionMark() && Name
[0] == '?')
50 if (PrefixTy
== Private
)
51 OS
<< DL
.getPrivateGlobalPrefix();
52 else if (PrefixTy
== LinkerPrivate
)
53 OS
<< DL
.getLinkerPrivateGlobalPrefix();
58 // If this is a simple string that doesn't need escaping, just append it.
62 static void getNameWithPrefixImpl(raw_ostream
&OS
, const Twine
&GVName
,
64 ManglerPrefixTy PrefixTy
) {
65 char Prefix
= DL
.getGlobalPrefix();
66 return getNameWithPrefixImpl(OS
, GVName
, PrefixTy
, DL
, Prefix
);
69 void Mangler::getNameWithPrefix(raw_ostream
&OS
, const Twine
&GVName
,
70 const DataLayout
&DL
) {
71 return getNameWithPrefixImpl(OS
, GVName
, DL
, Default
);
74 void Mangler::getNameWithPrefix(SmallVectorImpl
<char> &OutName
,
75 const Twine
&GVName
, const DataLayout
&DL
) {
76 raw_svector_ostream
OS(OutName
);
77 char Prefix
= DL
.getGlobalPrefix();
78 return getNameWithPrefixImpl(OS
, GVName
, Default
, DL
, Prefix
);
81 static bool hasByteCountSuffix(CallingConv::ID CC
) {
83 case CallingConv::X86_FastCall
:
84 case CallingConv::X86_StdCall
:
85 case CallingConv::X86_VectorCall
:
92 /// Microsoft fastcall and stdcall functions require a suffix on their name
93 /// indicating the number of words of arguments they take.
94 static void addByteCountSuffix(raw_ostream
&OS
, const Function
*F
,
95 const DataLayout
&DL
) {
96 // Calculate arguments size total.
97 unsigned ArgWords
= 0;
98 for (Function::const_arg_iterator AI
= F
->arg_begin(), AE
= F
->arg_end();
100 Type
*Ty
= AI
->getType();
101 // 'Dereference' type in case of byval or inalloca parameter attribute.
102 if (AI
->hasByValOrInAllocaAttr())
103 Ty
= cast
<PointerType
>(Ty
)->getElementType();
104 // Size should be aligned to pointer size.
105 unsigned PtrSize
= DL
.getPointerSize();
106 ArgWords
+= alignTo(DL
.getTypeAllocSize(Ty
), PtrSize
);
109 OS
<< '@' << ArgWords
;
112 void Mangler::getNameWithPrefix(raw_ostream
&OS
, const GlobalValue
*GV
,
113 bool CannotUsePrivateLabel
) const {
114 ManglerPrefixTy PrefixTy
= Default
;
115 if (GV
->hasPrivateLinkage()) {
116 if (CannotUsePrivateLabel
)
117 PrefixTy
= LinkerPrivate
;
122 const DataLayout
&DL
= GV
->getParent()->getDataLayout();
123 if (!GV
->hasName()) {
124 // Get the ID for the global, assigning a new one if we haven't got one
126 unsigned &ID
= AnonGlobalIDs
[GV
];
128 ID
= AnonGlobalIDs
.size();
130 // Must mangle the global into a unique ID.
131 getNameWithPrefixImpl(OS
, "__unnamed_" + Twine(ID
), DL
, PrefixTy
);
135 StringRef Name
= GV
->getName();
136 char Prefix
= DL
.getGlobalPrefix();
138 // Mangle functions with Microsoft calling conventions specially. Only do
139 // this mangling for x86_64 vectorcall and 32-bit x86.
140 const Function
*MSFunc
= dyn_cast
<Function
>(GV
);
142 // Don't add byte count suffixes when '\01' or '?' are in the first
144 if (Name
.startswith("\01") ||
145 (DL
.doNotMangleLeadingQuestionMark() && Name
.startswith("?")))
149 MSFunc
? MSFunc
->getCallingConv() : (unsigned)CallingConv::C
;
150 if (!DL
.hasMicrosoftFastStdCallMangling() &&
151 CC
!= CallingConv::X86_VectorCall
)
154 if (CC
== CallingConv::X86_FastCall
)
155 Prefix
= '@'; // fastcall functions have an @ prefix instead of _.
156 else if (CC
== CallingConv::X86_VectorCall
)
157 Prefix
= '\0'; // vectorcall functions have no prefix.
160 getNameWithPrefixImpl(OS
, Name
, PrefixTy
, DL
, Prefix
);
165 // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
166 // or vectorcall, add it. These functions have a suffix of @N where N is the
167 // cumulative byte size of all of the parameters to the function in decimal.
168 if (CC
== CallingConv::X86_VectorCall
)
169 OS
<< '@'; // vectorcall functions use a double @ suffix.
170 FunctionType
*FT
= MSFunc
->getFunctionType();
171 if (hasByteCountSuffix(CC
) &&
172 // "Pure" variadic functions do not receive @0 suffix.
173 (!FT
->isVarArg() || FT
->getNumParams() == 0 ||
174 (FT
->getNumParams() == 1 && MSFunc
->hasStructRetAttr())))
175 addByteCountSuffix(OS
, MSFunc
, DL
);
178 void Mangler::getNameWithPrefix(SmallVectorImpl
<char> &OutName
,
179 const GlobalValue
*GV
,
180 bool CannotUsePrivateLabel
) const {
181 raw_svector_ostream
OS(OutName
);
182 getNameWithPrefix(OS
, GV
, CannotUsePrivateLabel
);
185 void llvm::emitLinkerFlagsForGlobalCOFF(raw_ostream
&OS
, const GlobalValue
*GV
,
186 const Triple
&TT
, Mangler
&Mangler
) {
187 if (!GV
->hasDLLExportStorageClass() || GV
->isDeclaration())
190 if (TT
.isKnownWindowsMSVCEnvironment())
195 if (TT
.isWindowsGNUEnvironment() || TT
.isWindowsCygwinEnvironment()) {
197 raw_string_ostream
FlagOS(Flag
);
198 Mangler
.getNameWithPrefix(FlagOS
, GV
, false);
200 if (Flag
[0] == GV
->getParent()->getDataLayout().getGlobalPrefix())
201 OS
<< Flag
.substr(1);
205 Mangler
.getNameWithPrefix(OS
, GV
, false);
208 if (!GV
->getValueType()->isFunctionTy()) {
209 if (TT
.isKnownWindowsMSVCEnvironment())
216 void llvm::emitLinkerFlagsForUsedCOFF(raw_ostream
&OS
, const GlobalValue
*GV
,
217 const Triple
&T
, Mangler
&M
) {
218 if (!T
.isKnownWindowsMSVCEnvironment())
222 M
.getNameWithPrefix(OS
, GV
, false);