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/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"
25 enum ManglerPrefixTy
{
26 Default
, ///< Emit default string before each symbol.
27 Private
, ///< Emit "private" prefix before each symbol.
28 LinkerPrivate
///< Emit "linker private" prefix before each symbol.
32 static void getNameWithPrefixImpl(raw_ostream
&OS
, const Twine
&GVName
,
33 ManglerPrefixTy PrefixTy
,
34 const DataLayout
&DL
, char Prefix
) {
35 SmallString
<256> TmpData
;
36 StringRef Name
= GVName
.toStringRef(TmpData
);
37 assert(!Name
.empty() && "getNameWithPrefix requires non-empty name");
39 // No need to do anything special if the global has the special "do not
40 // mangle" flag in the name.
41 if (Name
[0] == '\1') {
46 if (PrefixTy
== Private
)
47 OS
<< DL
.getPrivateGlobalPrefix();
48 else if (PrefixTy
== LinkerPrivate
)
49 OS
<< DL
.getLinkerPrivateGlobalPrefix();
54 // If this is a simple string that doesn't need escaping, just append it.
58 static void getNameWithPrefixImpl(raw_ostream
&OS
, const Twine
&GVName
,
60 ManglerPrefixTy PrefixTy
) {
61 char Prefix
= DL
.getGlobalPrefix();
62 return getNameWithPrefixImpl(OS
, GVName
, PrefixTy
, DL
, Prefix
);
65 void Mangler::getNameWithPrefix(raw_ostream
&OS
, const Twine
&GVName
,
66 const DataLayout
&DL
) {
67 return getNameWithPrefixImpl(OS
, GVName
, DL
, Default
);
70 void Mangler::getNameWithPrefix(SmallVectorImpl
<char> &OutName
,
71 const Twine
&GVName
, const DataLayout
&DL
) {
72 raw_svector_ostream
OS(OutName
);
73 char Prefix
= DL
.getGlobalPrefix();
74 return getNameWithPrefixImpl(OS
, GVName
, Default
, DL
, Prefix
);
77 static bool hasByteCountSuffix(CallingConv::ID CC
) {
79 case CallingConv::X86_FastCall
:
80 case CallingConv::X86_StdCall
:
81 case CallingConv::X86_VectorCall
:
88 /// Microsoft fastcall and stdcall functions require a suffix on their name
89 /// indicating the number of words of arguments they take.
90 static void addByteCountSuffix(raw_ostream
&OS
, const Function
*F
,
91 const DataLayout
&DL
) {
92 // Calculate arguments size total.
93 unsigned ArgWords
= 0;
94 for (Function::const_arg_iterator AI
= F
->arg_begin(), AE
= F
->arg_end();
96 Type
*Ty
= AI
->getType();
97 // 'Dereference' type in case of byval or inalloca parameter attribute.
98 if (AI
->hasByValOrInAllocaAttr())
99 Ty
= cast
<PointerType
>(Ty
)->getElementType();
100 // Size should be aligned to pointer size.
101 unsigned PtrSize
= DL
.getPointerSize();
102 ArgWords
+= RoundUpToAlignment(DL
.getTypeAllocSize(Ty
), PtrSize
);
105 OS
<< '@' << ArgWords
;
108 void Mangler::getNameWithPrefix(raw_ostream
&OS
, const GlobalValue
*GV
,
109 bool CannotUsePrivateLabel
) const {
110 ManglerPrefixTy PrefixTy
= Default
;
111 if (GV
->hasPrivateLinkage()) {
112 if (CannotUsePrivateLabel
)
113 PrefixTy
= LinkerPrivate
;
118 const DataLayout
&DL
= GV
->getParent()->getDataLayout();
119 if (!GV
->hasName()) {
120 // Get the ID for the global, assigning a new one if we haven't got one
122 unsigned &ID
= AnonGlobalIDs
[GV
];
124 ID
= NextAnonGlobalID
++;
126 // Must mangle the global into a unique ID.
127 getNameWithPrefixImpl(OS
, "__unnamed_" + Twine(ID
), DL
, PrefixTy
);
131 StringRef Name
= GV
->getName();
132 char Prefix
= DL
.getGlobalPrefix();
134 // Mangle functions with Microsoft calling conventions specially. Only do
135 // this mangling for x86_64 vectorcall and 32-bit x86.
136 const Function
*MSFunc
= dyn_cast
<Function
>(GV
);
137 if (Name
.startswith("\01"))
138 MSFunc
= nullptr; // Don't mangle when \01 is present.
140 MSFunc
? MSFunc
->getCallingConv() : (unsigned)CallingConv::C
;
141 if (!DL
.hasMicrosoftFastStdCallMangling() &&
142 CC
!= CallingConv::X86_VectorCall
)
145 if (CC
== CallingConv::X86_FastCall
)
146 Prefix
= '@'; // fastcall functions have an @ prefix instead of _.
147 else if (CC
== CallingConv::X86_VectorCall
)
148 Prefix
= '\0'; // vectorcall functions have no prefix.
151 getNameWithPrefixImpl(OS
, Name
, PrefixTy
, DL
, Prefix
);
156 // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
157 // or vectorcall, add it. These functions have a suffix of @N where N is the
158 // cumulative byte size of all of the parameters to the function in decimal.
159 if (CC
== CallingConv::X86_VectorCall
)
160 OS
<< '@'; // vectorcall functions use a double @ suffix.
161 FunctionType
*FT
= MSFunc
->getFunctionType();
162 if (hasByteCountSuffix(CC
) &&
163 // "Pure" variadic functions do not receive @0 suffix.
164 (!FT
->isVarArg() || FT
->getNumParams() == 0 ||
165 (FT
->getNumParams() == 1 && MSFunc
->hasStructRetAttr())))
166 addByteCountSuffix(OS
, MSFunc
, DL
);
169 void Mangler::getNameWithPrefix(SmallVectorImpl
<char> &OutName
,
170 const GlobalValue
*GV
,
171 bool CannotUsePrivateLabel
) const {
172 raw_svector_ostream
OS(OutName
);
173 getNameWithPrefix(OS
, GV
, CannotUsePrivateLabel
);