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 (PrefixTy
== Private
)
48 OS
<< DL
.getPrivateGlobalPrefix();
49 else if (PrefixTy
== LinkerPrivate
)
50 OS
<< DL
.getLinkerPrivateGlobalPrefix();
55 // If this is a simple string that doesn't need escaping, just append it.
59 static void getNameWithPrefixImpl(raw_ostream
&OS
, const Twine
&GVName
,
61 ManglerPrefixTy PrefixTy
) {
62 char Prefix
= DL
.getGlobalPrefix();
63 return getNameWithPrefixImpl(OS
, GVName
, PrefixTy
, DL
, Prefix
);
66 void Mangler::getNameWithPrefix(raw_ostream
&OS
, const Twine
&GVName
,
67 const DataLayout
&DL
) {
68 return getNameWithPrefixImpl(OS
, GVName
, DL
, Default
);
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
);
78 static bool hasByteCountSuffix(CallingConv::ID CC
) {
80 case CallingConv::X86_FastCall
:
81 case CallingConv::X86_StdCall
:
82 case CallingConv::X86_VectorCall
:
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();
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
);
106 OS
<< '@' << ArgWords
;
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
;
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
123 unsigned &ID
= AnonGlobalIDs
[GV
];
125 ID
= AnonGlobalIDs
.size();
127 // Must mangle the global into a unique ID.
128 getNameWithPrefixImpl(OS
, "__unnamed_" + Twine(ID
), DL
, PrefixTy
);
132 StringRef Name
= GV
->getName();
133 char Prefix
= DL
.getGlobalPrefix();
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.
141 MSFunc
? MSFunc
->getCallingConv() : (unsigned)CallingConv::C
;
142 if (!DL
.hasMicrosoftFastStdCallMangling() &&
143 CC
!= CallingConv::X86_VectorCall
)
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.
152 getNameWithPrefixImpl(OS
, Name
, PrefixTy
, DL
, Prefix
);
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
);
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
);
177 void llvm::emitLinkerFlagsForGlobalCOFF(raw_ostream
&OS
, const GlobalValue
*GV
,
178 const Triple
&TT
, Mangler
&Mangler
) {
179 if (!GV
->hasDLLExportStorageClass() || GV
->isDeclaration())
182 if (TT
.isKnownWindowsMSVCEnvironment())
187 if (TT
.isWindowsGNUEnvironment() || TT
.isWindowsCygwinEnvironment()) {
189 raw_string_ostream
FlagOS(Flag
);
190 Mangler
.getNameWithPrefix(FlagOS
, GV
, false);
192 if (Flag
[0] == GV
->getParent()->getDataLayout().getGlobalPrefix())
193 OS
<< Flag
.substr(1);
197 Mangler
.getNameWithPrefix(OS
, GV
, false);
200 if (!GV
->getValueType()->isFunctionTy()) {
201 if (TT
.isKnownWindowsMSVCEnvironment())