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/Support/raw_ostream.h"
23 static void getNameWithPrefixImpl(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");
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') {
37 if (PrefixTy == Mangler::Private)
38 OS << DL.getPrivateGlobalPrefix();
39 else if (PrefixTy == Mangler::LinkerPrivate)
40 OS << DL.getLinkerPrivateGlobalPrefix();
45 // If this is a simple string that doesn't need escaping, just append it.
49 void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
50 ManglerPrefixTy PrefixTy) const {
51 char Prefix = DL->getGlobalPrefix();
52 return getNameWithPrefixImpl(OS, GVName, PrefixTy, *DL, Prefix);
55 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
56 const Twine &GVName, const DataLayout &DL) {
57 raw_svector_ostream OS(OutName);
58 char Prefix = DL.getGlobalPrefix();
59 return getNameWithPrefixImpl(OS, GVName, Mangler::Default, DL, Prefix);
62 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
64 ManglerPrefixTy PrefixTy) const {
65 raw_svector_ostream OS(OutName);
66 char Prefix = DL->getGlobalPrefix();
67 return getNameWithPrefixImpl(OS, GVName, PrefixTy, *DL, Prefix);
70 static bool hasByteCountSuffix(CallingConv::ID CC) {
72 case CallingConv::X86_FastCall:
73 case CallingConv::X86_StdCall:
74 case CallingConv::X86_VectorCall:
81 /// Microsoft fastcall and stdcall functions require a suffix on their name
82 /// indicating the number of words of arguments they take.
83 static void addByteCountSuffix(raw_ostream &OS, const Function *F,
84 const DataLayout &DL) {
85 // Calculate arguments size total.
86 unsigned ArgWords = 0;
87 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
89 Type *Ty = AI->getType();
90 // 'Dereference' type in case of byval or inalloca parameter attribute.
91 if (AI->hasByValOrInAllocaAttr())
92 Ty = cast<PointerType>(Ty)->getElementType();
93 // Size should be aligned to pointer size.
94 unsigned PtrSize = DL.getPointerSize();
95 ArgWords += RoundUpToAlignment(DL.getTypeAllocSize(Ty), PtrSize);
98 OS << '@' << ArgWords;
101 void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
102 bool CannotUsePrivateLabel) const {
103 ManglerPrefixTy PrefixTy = Mangler::Default;
104 if (GV->hasPrivateLinkage()) {
105 if (CannotUsePrivateLabel)
106 PrefixTy = Mangler::LinkerPrivate;
108 PrefixTy = Mangler::Private;
111 if (!GV->hasName()) {
112 // Get the ID for the global, assigning a new one if we haven't got one
114 unsigned &ID = AnonGlobalIDs[GV];
116 ID = NextAnonGlobalID++;
118 // Must mangle the global into a unique ID.
119 getNameWithPrefix(OS, "__unnamed_" + Twine(ID), PrefixTy);
123 StringRef Name = GV->getName();
124 char Prefix = DL->getGlobalPrefix();
126 // Mangle functions with Microsoft calling conventions specially. Only do
127 // this mangling for x86_64 vectorcall and 32-bit x86.
128 const Function *MSFunc = dyn_cast<Function>(GV);
129 if (Name.startswith("\01"))
130 MSFunc = nullptr; // Don't mangle when \01 is present.
132 MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C;
133 if (!DL->hasMicrosoftFastStdCallMangling() &&
134 CC != CallingConv::X86_VectorCall)
137 if (CC == CallingConv::X86_FastCall)
138 Prefix = '@'; // fastcall functions have an @ prefix instead of _.
139 else if (CC == CallingConv::X86_VectorCall)
140 Prefix = '\0'; // vectorcall functions have no prefix.
143 getNameWithPrefixImpl(OS, Name, PrefixTy, *DL, Prefix);
148 // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
149 // or vectorcall, add it. These functions have a suffix of @N where N is the
150 // cumulative byte size of all of the parameters to the function in decimal.
151 if (CC == CallingConv::X86_VectorCall)
152 OS << '@'; // vectorcall functions use a double @ suffix.
153 FunctionType *FT = MSFunc->getFunctionType();
154 if (hasByteCountSuffix(CC) &&
155 // "Pure" variadic functions do not receive @0 suffix.
156 (!FT->isVarArg() || FT->getNumParams() == 0 ||
157 (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
158 addByteCountSuffix(OS, MSFunc, *DL);
161 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
162 const GlobalValue *GV,
163 bool CannotUsePrivateLabel) const {
164 raw_svector_ostream OS(OutName);
165 getNameWithPrefix(OS, GV, CannotUsePrivateLabel);